Spring Cloud封装Feign,直接使用Spring MVC注解以及HttpMessageConverters
来序列化。
Spring Boot有autoconfigure机制,当spring boot中引入了spring-web jar包的时候,HttpMessageConvertersAutoConfiguration便会起作用,另外当引入了ObjectMapper类时候,MappingJackson2HttpMessageConverterConfiguration便会起作用,从而自动加入了HttpMessageConverters
中,这样feign就会直接来序列化json了。
在Declarative REST Client: Feign中有这么一段话
FooConfiguration does not need to be annotated with @Configuration. However, if it is, then take care to exclude it from any @ComponentScan that would otherwise include this configuration as it will become the default source for feign.Decoder, feign.Encoder, feign.Contract, etc., when specified. This can be avoided by putting it in a separate, non-overlapping package from any @ComponentScan or @SpringBootApplication, or it can be explicitly excluded in @ComponentScan.
大致意思是,FeignClient注解上可以指定configuration属性,但是对于指定的configuration属性的类不需要使用@Configuration注解,不然这个里面设置的属性将会覆盖默认的配置属性,转而使用该configuration配置的。如果不使用@Configuration注解,那么只有该FeignClient会使用该configuration。