我有一个类定义为:
@JsonRootName(value = "component")
public class ComponentBean{
//...private variables....getters....settters....default constructor not shown for brevity
我使用 Springs基于 java 的配置来配置我的 ObjectMapper:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
... other config not shown for brevity
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> pArgumentResolvers) {
pArgumentResolvers.add(this.getCurrentUserHandlerMethodArgumentResolver());
}
@Bean
public ObjectMapper getJackson2ObjectMapperFactoryBean() {
final Jackson2ObjectMapperFactoryBean mapper = new Jackson2ObjectMapperFactoryBean();
mapper.setIndentOutput(true);
mapper.setSimpleDateFormat("MM/dd/yyyy HH:mm:ss");
mapper.setFeaturesToEnable(DeserializationFeature.UNWRAP_ROOT_VALUE);
mapper.setFeaturesToDisable(SerializationFeature.INDENT_OUTPUT);
mapper.setFeaturesToEnable(SerializationFeature.WRAP_ROOT_VALUE);
final Map<Class<?>, JsonDeserializer<?>> deserializers = new HashMap<Class<?>, JsonDeserializer<?>>();
deserializers.put(...) // customer deserializers -- ComponentBean is not one of them
mapper.setDeserializersByType(deserializers);
mapper.afterPropertiesSet();
final ObjectMapper objectMapper = mapper.getObject();
objectMapper.registerModule(new JodaModule()); // register custom joda time serializer
return objectMapper;
}
@Bean
public MappingJackson2HttpMessageConverter getMappingJackson2HttpMessageConverter() {
final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(this.getJackson2ObjectMapperFactoryBean());
return converter;
}
使用 java 配置,在尝试从我的应用程序创建新的 ComponentBean 时,我收到异常:
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "component" (class com.package.components.ComponentBean), not marked as ignorable (11 known properties: ....all my the properties of the ComponentBean not shown....])
现在,这与我之前使用基于 xml 的配置进行配置的方式相同,但我切换到使用基于 java 的配置——这似乎是我无法正常工作的唯一配置部分。
我的基于 xml 的配置是这样的:
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="com.aoins.sales.user.CurrentUserHandlerMethodArgumentResolver" />
</mvc:argument-resolvers>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="objectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="objectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
p:indentOutput="true" p:simpleDateFormat="MM/dd/yyyy HH:mm:ss">
<property name="featuresToEnable">
<array>
<util:constant static-field="com.fasterxml.jackson.databind.DeserializationFeature.UNWRAP_ROOT_VALUE" />
</array>
</property>
<property name="featuresToDisable">
<array>
<util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT" />
</array>
</property>
<!-- Add deserializers that require autowiring -->
<property name="deserializersByType">
<map key-type="java.lang.Class">
<entry key="...">
<bean class="..." />
</entry>
</map>
</property>
</bean>
我的问题是如何使用基于 java 的配置使其正常工作。我是否缺少用于识别 json 根的 spring 注释?是否需要为我的 ObjectMapper 添加另一个注释、其他设置,还是我只需要为我的 ComponentBean 创建一个自定义反序列化器(我试图避免为 ComponentBean 创建一个自定义反序列化器,因为使用@JsonRootName 应该可以工作)?
有些事情需要注意,我不想忽略这些属性中的任何一个,因为我需要全部使用它们。