Spring MVC 3.2和JSON ObjectMapper问题 [英] Spring MVC 3.2 and JSON ObjectMapper issue
问题描述
我最近将Spring版本从3.1.2升级到了3.2.0.我发现JSON属性(例如,包装根元素)可防止ObjectMapper中定义的null值不再起作用.
I have recently upgraded my Spring version to 3.2.0 from 3.1.2. I find that that JSON properties like wrap root element, prevent null values that are defined in ObjectMapper are not working anymore.
这是代码段
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true" />
<property name="ignoreAcceptHeader" value="false" />
<property name="mediaTypes" >
<value>
json=application/json
xml=application/xml
</value>
</property>
</bean>
和JSON转换器
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="customJacksonObjectMapper"/>
<property name="supportedMediaTypes" value="application/json"/>
</bean>
对象映射器代码
public class CustomJacksonObjectMapper extends ObjectMapper {
@SuppressWarnings("deprecation")
public CustomJacksonObjectMapper() {
super();
final AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
this.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
this.configure(org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
this.configure(org.codehaus.jackson.map.SerializationConfig.Feature.WRITE_NULL_PROPERTIES, false);
this.setDeserializationConfig(this.getDeserializationConfig().withAnnotationIntrospector(introspector));
this.setSerializationConfig(this.getSerializationConfig().withAnnotationIntrospector(introspector));
}
}
杰克逊版
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-xc</artifactId>
<version>1.9.7</version>
</dependency>
可能是什么问题?任何指针都表示赞赏.
What could be the issue? Any pointers are appreciated.
推荐答案
免责声明:我无法确定问题代码为何无法正常工作,但可以重现该问题.这个答案的确为我提供了另一种方法.
可能是一个错误,因为我可以使用显式配置重现这两个问题:
It could be a bug, as I can reproduce the problem with both with explicit config:
<bean id="jacksonObjectMapper" class="com.demo.CustomJacksonObjectMapper"/>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper"/>
<property name="supportedMediaTypes" value="application/json"/>
</bean>
并通过 mvc:message-converter
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
在使用示例类时,两者都给我{"foo":null,"bar":"bar"}
where both give me {"foo":null,"bar":"bar"}
when using the example class
Demo.java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.codehaus.jackson.annotate.JsonProperty;
@Controller
@RequestMapping("/data")
public class Data {
@RequestMapping
@ResponseBody
public Dummy dataIndex() {
return new Dummy();
}
public class Dummy {
String foo = null;
@JsonProperty
public String foo() {
return foo;
}
String bar = "bar";
@JsonProperty
public String bar() {
return bar;
}
}
}
但是,我本以为 mvc:message-converter 方法会起作用,只是因为我看到了覆盖<mvc:annotation-driven/>
执行的默认bean注册的问题(请参见
However, I would have thought the mvc:message-converter method would work, only because I have seen issues overriding the default bean registration that <mvc:annotation-driven/>
performs (see Web MVC Framework) and using the nested configuration is preferred(?).
Maybe the Jackson 2 support has caused some backwards compatibility issues with Jackson 1?
但是,切换到 MappingJackson2HttpMessageConverter (在Spring 3.1.2和Spring 3.2中受支持),我 am 能够更改ObjectMapper配置以不写入空值并包装JSON输出. .但是,仅当使用<mvc:message-converters/>
!
However, switching to the MappingJackson2HttpMessageConverter (supported in Spring 3.1.2 and Spring 3.2), I am able to alter the ObjectMapper configuration to not write null values and wrap the JSON output... but only when using the config inside the <mvc:message-converters/>
!
我得到{"Dummy":{"bar":"bar"}}
并进行了以下更改:
I get {"Dummy":{"bar":"bar"}}
with the following changes:
pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.1.0</version>
</dependency>
CustomJacksonObjectMapper.java
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.SerializationFeature;
import static com.fasterxml.jackson.annotation.JsonInclude.*;
public class CustomJacksonObjectMapper extends ObjectMapper {
public CustomJacksonObjectMapper() {
super();
this.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
this.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
this.setSerializationInclusion(Include.NON_NULL);
}
}
Demo.java 切换到Jackson 2的新程序包结构
Demo.java switch to new package structure for Jackson 2
import com.fasterxml.jackson.annotation.JsonProperty;
demo-servlet.xml
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
最后,根据存在于您的代码中的原因.在Jackson> = v2.0中,已将其删除,因此 CustomJacksonObjectMapper.java 示例中的代码更改.
Lastly, according to the SerializationConfig.Feature
documentation, WRITE_NULL_PROPERTIES
feature is deprecated < v2.0 and you should be using SerializationConfig.setSerializationInclusion()
anyway. I assume this is why the @SuppressWarnings("deprecation")
exists in your code. In Jackson >= v2.0, it has been removed, hence the code change in the CustomJacksonObjectMapper.java example.
在Spring中配置ObjectMapper 提出了另一种解决方案.
Configuring ObjectMapper in Spring proposes an alternate solution.
希望有帮助!
这篇关于Spring MVC 3.2和JSON ObjectMapper问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!