【发布时间】:2019-12-22 14:06:02
【问题描述】:
我正在尝试使用 2 个不同的 Jackson ObjectMappers,以便我可以在代码中使用其中任何一个进行切换。 2 个ObjectMappers 的配置会有一些细微差别。
在我的配置文件中,我将它们作为 2 种不同的方式:
@Configuration
public class ApplicationConfiguration {
private ObjectMapper createMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
// And a few more other common configurations
return mapper;
}
@Bean
@Qualifier("standardObjectMapper")
public ObjectMapper standardObjectMapper() {
ObjectMapper mapper = createMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(Product.class, new StandardProductSerializer());
mapper.registerModule(module);
return mapper;
}
@Bean
@Qualifier("specialObjectMapper")
public ObjectMapper specialObjectMapper() {
ObjectMapper mapper = createMapper();
SimpleModule module = new SimpleModule();
// In this mapper, I have a different serializer for the Product class
module.addSerializer(Product.class, new SpecialProductSerializer());
mapper.registerModule(module);
return mapper;
}
}
我的计划是在需要时注入并使用其中一个映射器。所以在我的测试中,我有这样的东西:
class SerializationTest {
@Autowired
@Qualifier("standardObjectMapper")
private ObjectMapper standardObjectMapper;
@Autowired
@Qualifier("specialObjectMapper")
private ObjectMapper specialObjectMapper;
@Test
void testSerialization() throws JsonProcessingException {
Product myProduct = new Product("Test Product");
String stdJson = objectMapper.writeValueAsString(myProduct);
String specialJson = specialObjectMapper.writeValueAsString(myProduct);
// Both stdJson and specialJson are of the same value even though they should be different because the mappers have different serializers!
}
}
但是,2 个ObjectMappers、standardObjectMapper 和specialObjectMapper 似乎都使用相同的StandardProductSerializer。
我希望 specialObjectMapper 使用 SpecialProductSerializer,但事实并非如此。
这 2 个 ObjectMapper 不应该不同吗?我假设注入将基于他们各自的名字,因为他们的类型是相同的?
我应该如何解决这个问题,以便 2 个 ObjectMapper 可以使用不同的序列化器?
更新:
我尝试过使用 Qualifiers,但我收到一个错误,即找不到 bean:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.site.SerializationTest': Unsatisfied dependency expressed through field 'standardObjectMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.fasterxml.jackson.databind.ObjectMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value="standardObjectMapper")}
【问题讨论】:
-
你试过用Qualifiers来区分bean吗?顺便说一句:我建议使用构造函数或设置器注入。您可以使用
@Mock和@InjectMocks轻松注入字段,但这会增加测试的执行时间。 -
@Turing85 我尝试使用限定符,但出现以下错误:
Unsatisfied dependency expressed through field 'standardObjectMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.fasterxml.jackson.databind.ObjectMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value="standardObjectMapper")} -
您是否使用
@Qualifier(...)注释了注入点和 bean-method? -
@Turing85 是的,我已经使用限定符更新了我的问题。这两个地方我都放了,名字都一样。
-
你能确认beans已经创建好了,即这两个方法是在启动的时候执行的吗?
标签: java spring spring-boot jackson