我有一個類定義爲:@JsonRootName/UnrecognizedPropertyException:無法識別的領域... /使用基於Java的配置
@JsonRootName(value = "component")
public class ComponentBean{
//...private variables....getters....settters....default constructor not shown for brevity
我用溫泉來配置我的ObjectMapper這樣基於Java的配置:
@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的配置正常工作。我是否錯過了Spring的註釋來識別json根?是否還需要爲我的ObjectMapper添加另一個註釋,附加設置,還是我只需要爲我的ComponentBean創建一個自定義反序列化器(我試圖避免爲ComponentBean創建一個自定義反序列化器,因爲使用@JsonRootName應該工作)?
有些事情要注意,我不想忽略任何這些屬性,因爲我需要全部使用它們。