Chinaunix首页 | 论坛 | 博客
  • 博客访问: 823780
  • 博文数量: 247
  • 博客积分: 166
  • 博客等级: 入伍新兵
  • 技术积分: 2199
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-15 16:10
文章分类

全部博文(247)

文章存档

2017年(1)

2015年(63)

2014年(80)

2013年(94)

2012年(9)

分类: Java

2015-07-15 13:55:39

一、使用Spring内置的支持,利用spring mvc3的注解@ResponseBody配置的json

其他人商议用json通信,因为对方是php程序员,php的编码风格对于java程序员来说就是变态,有几点变态需求:

1、json requestbody 输要用单引号(jackson默认是双引号,单引号直接报错)

2、responsebody数字输出要加引号,字段名要加引号。

3、null值为空串

 

于是查阅官方文档,终于找到方法,有点小搓,直接在Converter对象上注入一个自定义类ObjectMappingCustomer:

点击(此处)折叠或打开

  1. <bean class=\"org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter\">
  2.         <property name=\"messageConverters\">
  3.             <list>
  4.                 <bean class=\"org.springframework.http.converter.json.MappingJacksonHttpMessageConverter\">
  5.                     <property name=\"supportedMediaTypes\">
  6.                         <list>
  7.                             <value>text/plain;charset=UTF-8</value>
  8.                             <value>application/json;charset=UTF-8</value>
  9.                         </list>
  10.                     </property>
  11.                     <property name=\"objectMapper\">
  12.                         <bean class=\"com.wuliaolll.util.ObjectMappingCustomer\"></bean>
  13.                     </property>
  14.                 </bean>
  15.             </list>
  16.         </property>
  17.     </bean>

点击(此处)折叠或打开

  1. package com.wuliaolll.util;

  2. import java.io.IOException;

  3. import org.codehaus.jackson.JsonGenerator;
  4. import org.codehaus.jackson.JsonParser;
  5. import org.codehaus.jackson.JsonProcessingException;
  6. import org.codehaus.jackson.map.JsonSerializer;
  7. import org.codehaus.jackson.map.ObjectMapper;
  8. import org.codehaus.jackson.map.SerializerProvider;

  9. public class ObjectMappingCustomer extends ObjectMapper
  10. {

  11.     public ObjectMappingCustomer()
  12.     {
  13.         super();
  14.         // 允许单引号
  15.         this.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
  16.         // 字段和值都加引号
  17.         this.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
  18.         // 数字也加引号
  19.         this.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true);
  20.         this.configure(JsonGenerator.Feature.QUOTE_NON_NUMERIC_NUMBERS, true);
  21.         // 空值处理为空串
  22.         this.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>()
  23.         {

  24.             @Override
  25.             public void serialize(
  26.                     Object value,
  27.                     JsonGenerator jg,
  28.                     SerializerProvider sp) throws IOException, JsonProcessingException
  29.             {
  30.                 jg.writeString("");
  31.             }
  32.         });

  33.     }
  34. }

二、
json视图方式

点击(此处)折叠或打开

  1. <bean class=\"org.springframework.web.servlet.view.json.MappingJacksonJsonView\">
  2.       <property name=\"encoding\">
  3.           <value type=\"org.codehaus.jackson.JsonEncoding\">UTF8</value>
  4.       </property>
  5.       <property name=\"contentType\" value=\"application/json;charset=UTF-8\"/>
  6. </bean>
json:
{"s":{"userId":"1","userName":"测试","passWord":"3","list":null,"dep":null,"date":1325347200000},"userForm":{"userId":"1","userName":"测试","passWord":"3","list":null,"dep":null,"date":1325347200000}}
里面有null值,怎么配置,才能让null自动变成空串

自己构建ObjectMapper注入进去,在ObjectMapper中设置NOT_NULL不转换
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="extractValueFromSingleKeyModel" value="true" />
<property name="objectMapper">
<bean class="org.codehaus.jackson.map.ObjectMapper">
<property name="serializationInclusion">
<value type="org.codehaus.jackson.map.annotate.JsonSerialize$Inclusion">NON_NULL</value>
</property>
</bean>
</property>
</bean>

参考自: />
阅读(6887) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~