自定义对象映射器 bean 更改打开 Feign 客户端/自动配置的 objectMapper bean 的默认属性
Custom object mapper bean changing the default properties of open Feign Client / auto configured objectMapper bean
我有 spring 的 open feign 客户端配置如下:
public class AppFeignConfiguration
{
@Bean
public ErrorDecoder errorDecoder()
{
return new FeignErrorDecoder();
}
@Bean
public Logger.Level logger()
{
return Logger.Level.FULL;
}
@Bean
public Request.Options options()
{
return new Request.Options( 30000, 30000 );
}
}
我在 @FeignClient
中将其作为配置提供如下
@FeignClient ( value = "apigateway-service", configuration = AppFeignConfiguration.class)
我相信 FeignClient 具有用于反序列化响应的默认配置(可能是具有某些属性的 ObjectMapper)。
到目前为止一切都很好。一切都按预期工作。
然后我创建了一个自定义对象映射器并将其作为 bean 返回,如下所示:
@Configuration
public class ObjectMapperConfig
{
@Bean ( name = "plainObjectMapper")
public ObjectMapper plainObjectMapper()
{
return new ObjectMapper();
}
}
现在的问题是,这扰乱了 FeignClient
的默认反序列化配置。
意思是,没有自定义 plainObjectMapper
,它过去工作正常,没有任何 json 解析错误。但是在将自定义 plainObjectMapper
创建为 bean 之后,FeignClient
抛出异常说明一些未知属性或其他内容。
nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized field "xyz" (class abc.def.ghi.class), not marked as ignorable; nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException
我通过删除自定义 plainObjectMapper
确认了这一点,它和以前一样工作正常。
这好像很奇怪啊!我给了这个 bean 一个自定义名称,它不应该与 springboot.
创建的 bean 混淆
我该如何克服这种冲突?
这并不完全正确。
如果您查看 FeignClientsConfiguration
source,您会发现它的编码器依赖于 HttpMessageConverters
class。
那么,HttpMessageConvertersAutoConfiguration
(source)本身就依赖于JacksonAutoConfiguration
。
后者仅在上下文中没有手动创建的对象映射器时才创建默认的 ObjectMapper
bean(参见 @ConditionalOnMissingBean
on the corresponding method)——不同的 bean 名称无关紧要这种情况。
因此,Feign 隐含地依赖于这个自动配置的 ObjectMapper
,但只有在您创建自己的之前。然后它开始使用你的。与许多其他 spring bean 相同。
如果您确实需要拥有自己的 ObjectMapper
bean,那么您需要正确配置它。但是您可以通过配置属性自定义自动配置的 - 请参阅 docs.
我有 spring 的 open feign 客户端配置如下:
public class AppFeignConfiguration
{
@Bean
public ErrorDecoder errorDecoder()
{
return new FeignErrorDecoder();
}
@Bean
public Logger.Level logger()
{
return Logger.Level.FULL;
}
@Bean
public Request.Options options()
{
return new Request.Options( 30000, 30000 );
}
}
我在 @FeignClient
中将其作为配置提供如下
@FeignClient ( value = "apigateway-service", configuration = AppFeignConfiguration.class)
我相信 FeignClient 具有用于反序列化响应的默认配置(可能是具有某些属性的 ObjectMapper)。
到目前为止一切都很好。一切都按预期工作。
然后我创建了一个自定义对象映射器并将其作为 bean 返回,如下所示:
@Configuration
public class ObjectMapperConfig
{
@Bean ( name = "plainObjectMapper")
public ObjectMapper plainObjectMapper()
{
return new ObjectMapper();
}
}
现在的问题是,这扰乱了 FeignClient
的默认反序列化配置。
意思是,没有自定义 plainObjectMapper
,它过去工作正常,没有任何 json 解析错误。但是在将自定义 plainObjectMapper
创建为 bean 之后,FeignClient
抛出异常说明一些未知属性或其他内容。
nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized field "xyz" (class abc.def.ghi.class), not marked as ignorable; nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException
我通过删除自定义 plainObjectMapper
确认了这一点,它和以前一样工作正常。
这好像很奇怪啊!我给了这个 bean 一个自定义名称,它不应该与 springboot.
创建的 bean 混淆我该如何克服这种冲突?
这并不完全正确。
如果您查看 FeignClientsConfiguration
source,您会发现它的编码器依赖于 HttpMessageConverters
class。
那么,HttpMessageConvertersAutoConfiguration
(source)本身就依赖于JacksonAutoConfiguration
。
后者仅在上下文中没有手动创建的对象映射器时才创建默认的 ObjectMapper
bean(参见 @ConditionalOnMissingBean
on the corresponding method)——不同的 bean 名称无关紧要这种情况。
因此,Feign 隐含地依赖于这个自动配置的 ObjectMapper
,但只有在您创建自己的之前。然后它开始使用你的。与许多其他 spring bean 相同。
如果您确实需要拥有自己的 ObjectMapper
bean,那么您需要正确配置它。但是您可以通过配置属性自定义自动配置的 - 请参阅 docs.