[FAQ]jackson解决:null到"", 空值处理为空串
来源:互联网 发布:to是网络的意思是什么 编辑:程序博客网 时间:2024/05/29 17:30
1.一个比较头疼的问题:
这样之后,还是不能自动匹配规则把整个的JOSN转为:invoiceTitle: null,》invoiceTitle: "",
jackson + spring 支持rest接口,输出的JSON 对于null的字符串是invoiceTitle: null,但是接受方希望返回的是invoiceTitle: ""。解决方法重写ObjectMapper
2.代码如下:
public JsonMapper() {//this(Include.NON_EMPTY);// 空值处理为空串this.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {@Overridepublic void serialize(Object value, JsonGenerator jgen,SerializerProvider provider) throws IOException,JsonProcessingException {jgen.writeString("");}});}
3.配置如下:
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"><mvc:message-converters register-defaults="true"><!-- 将StringHttpMessageConverter的默认编码设为UTF-8 --><bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8" /></bean><!-- 将Jackson2HttpMessageConverter的默认格式化输出为true --><bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="com.sencloud.util.JsonMapper"/> </property> </bean> <!-- 使用XML格式输出数据 --> <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"> <constructor-arg> <bean class="org.springframework.oxm.xstream.XStreamMarshaller"> <property name="streamDriver"> <bean class="com.thoughtworks.xstream.io.xml.StaxDriver"/> </property> </bean> </constructor-arg> <property name="supportedMediaTypes" value="application/xml"></property></bean></mvc:message-converters>
这样之后,还是不能自动匹配规则把整个的JOSN转为:invoiceTitle: null,》invoiceTitle: "",
只能通过下面实例实现解决这个问题。
brf.setResultCode(0);brf.setRows(orderBlocks);brf.setResultMessage("succeed");JsonMapper jsonMapper = new JsonMapper();return jsonMapper.toJson(brf);
注意使用这种方式需要手动设置编码如下:
@RequestMapping(method = RequestMethod.GET, value = "/test",produces = "application/json; charset=utf-8")
不知为毛,不定位了。
0 0
- [FAQ]jackson解决:null到"", 空值处理为空串
- jackson解决:null到"", 空值处理为空串
- Jackson 处理 null 或者 空字符串
- jackson ObjectMapper 序列化成json; 属性值为null时转换为""空字符串
- Spring mvc3 jackson输出null为空字符串问题
- Atitit java读取tif文件为空null的解决 图像处理
- uploadify file null 为空 空值 uploadify-v3.0.1
- jackson 实体转json属性为NULL或者为空不参加序列化
- jackson 实体转json 为NULL或者为空不参加序列化
- jackson 实体转json 为NULL或者为空不参加序列化
- jackson 实体转json 为NULL或者为空不参加序列化
- jackson 实体转json 为NULL或者为空不参加序列化
- jackson 实体转json 为NULL或者为空不参加序列化
- jackson 实体转json属性为NULL或者为空不参加序列化
- jackson 实体转json属性为NULL或者为空不参加序列化
- jackson 实体转json 为NULL或者为空不参加序列化
- jackson 实体转json 为NULL或者为空不参加序列化
- jackson 实体转json 为NULL或者为空不参加序列化
- traits:Traits技术初探
- 函数递归的简单实例
- projecteuler---->problem=25----1000-digit Fibonacci number
- 第1次实验——NPC问题(回溯算法、聚类分析)
- HDU1106--排序
- [FAQ]jackson解决:null到"", 空值处理为空串
- python 语句补充
- shader 2: vertex, fragment, surf的区别
- jQuery插件之Resizable
- host-谷歌
- egg-hunter之——minalic开源WEB服务器学习
- 怎么让移动开发更高效
- 首部手机诞生至今已40多年了
- 深入Java单例模式