spa*_*pal 11 spring json spring-mvc
我最近将我的Spring版本从3.1.2升级到3.2.0.我发现像包装根元素这样的JSON属性,防止在ObjectMapper中定义的空值不再起作用.
这是代码片段
<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>
Run Code Online (Sandbox Code Playgroud)
和JSON转换器
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="customJacksonObjectMapper"/>
<property name="supportedMediaTypes" value="application/json"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
对象映射器代码
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));
}
}
Run Code Online (Sandbox Code Playgroud)
杰克逊版
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-xc</artifactId>
<version>1.9.7</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
可能是什么问题?任何指针都表示赞赏.
and*_*dyb 14
免责声明:我无法确定为什么有问题的代码不起作用,但我可以重现这个问题.这个答案确实提供了一种适合我的替代方法.
它可能是一个错误,因为我可以使用显式配置重现问题:
<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>
Run Code Online (Sandbox Code Playgroud)
并通过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>
Run Code Online (Sandbox Code Playgroud)
在{"foo":null,"bar":"bar"}
使用示例类时,两者都给了我
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我原本认为mvc:message-converter方法可行,只是因为我看到了覆盖<mvc:annotation-driven/>
执行的默认bean注册的问题(请参阅Web MVC框架),并且首选使用嵌套配置(?).
也许杰克逊2的支持导致杰克逊1的一些向后兼容性问题?
然而,切换到MappingJackson2HttpMessageConverter(在Spring 3.1.2和Spring 3.2的支持),我是能够改变ObjectMapper配置不写空值,敷JSON输出... 但使用内部的配置,只有当<mvc:message-converters/>
!
我得到{"Dummy":{"bar":"bar"}}
以下更改:
的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>
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
Demo.java切换到Jackson 2的新包结构
import com.fasterxml.jackson.annotation.JsonProperty;
Run Code Online (Sandbox Code Playgroud)
演示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>
Run Code Online (Sandbox Code Playgroud)
最后,根据SerializationConfig.Feature
文档,WRITE_NULL_PROPERTIES
功能已弃用<v2.0,SerializationConfig.setSerializationInclusion()
无论如何您应该使用.我假设这就是@SuppressWarnings("deprecation")
你的代码中存在的原因.在Jackson> = v2.0中,它已被删除,因此CustomJacksonObjectMapper.java示例中的代码更改.
在Spring中配置ObjectMapper提出了另一种解决方案.
希望能帮助到你!
归档时间: |
|
查看次数: |
39796 次 |
最近记录: |