Spring MVC 3.2和JSON ObjectMapper问题

编程入门 行业动态 更新时间:2024-10-23 15:30:56
本文介绍了Spring MVC 3.2和JSON ObjectMapper问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我最近将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注册的问题(请参见 Web MVC框架),最好使用嵌套配置(?).

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(?).

也许对杰克逊2的支持引起了杰克逊1的一些向后兼容性问题?

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>

最后,根据 SerializationConfig.Feature 文档,不推荐使用WRITE_NULL_PROPERTIES功能< v2.0,无论如何您都应该使用SerializationConfig.setSerializationInclusion().我认为这就是为什么@SuppressWarnings("deprecation")存在于您的代码中的原因.在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问题

本文发布于:2023-11-24 21:13:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626851.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MVC   Spring   ObjectMapper   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!