spring jack 使用自定义的objectMapper。或者在xml配置objectMapper参数
2015-09-07 17:04
561 查看
在spring-servlet.xml中配置json的转换器:
MyObjectMapper.java文件如下:
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <!-- objectMapper配置 --> <property name="objectMapper"> <bean class="com.test.MyObjectMapper" /> <!-- <bean class="com.fasterxml.jackson.databind.ObjectMapper"> 驼峰命名法转换为小写加下划线 <property name="propertyNamingStrategy"> <bean class="com.fasterxml.jackson.databind.PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy" /> </property> 为null字段时不输出 <property name="serializationInclusion"> <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value> </property> 禁用空对象转换json校验 <property name="configure"> <value type="com.fasterxml.jackson.databind.SerializationFeature">FAIL_ON_EMPTY_BEANS</value> </property> 忽略未知的字段 <property name="configure"> <value type="com.fasterxml.jackson.databind.DeserializationFeature">FAIL_ON_UNKNOWN_PROPERTIES</value> </property> </bean> --> </property> <!-- 支持的类型,编码 --> <property name="supportedMediaTypes"> <span style="white-space:pre"> </span><list> <span style="white-space:pre"> </span><value>application/json;charset=UTF-8</value> </list> <span style="white-space:pre"> </span></property> </bean>
MyObjectMapper.java文件如下:
public class MyObjectMapper extends ObjectMapper { private static final long serialVersionUID = 4402127997078513582L; public MyObjectMapper() { //设置null值不参与序列化(字段不被显示) this.setSerializationInclusion(Include.NON_NULL); // 禁用空对象转换json校验 this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); this.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); //驼峰命名法转换为小写加下划线 this.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); } }
相关文章推荐
- objective-c中的协议和类别
- 黑马程序员——Objective-C Foundation框架中的NSDirctionary类以及NSMutableDirctionary类
- 黑马程序员——Objective-C Foundation框架中的NSMutableString对象
- 黑马程序员——Objective-C Foundation框架中的NSString对象
- 黑马程序员——Objective-C Foundation框架中的NSObject对象
- Elasticsearch中的根对象(Root Object)
- 黑马程序员——Objective-C之特殊语法总结
- objective-c 中随机数的用法 (3种:arc4random() 、random()、CCRANDOM_0_1() )
- [__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x17deba00
- Objective-C中的@property和@synthesize用法
- Effective Objective C 2.0
- 【转】Objective-C 与 Runtime:为什么是这样?
- JSONObject与JSONArray的使用
- error LNK2001: unresolved external symbol "public: virtual struct QMetaObject
- Objective-C可变参数方法的定义和使用
- Objective C,UIScrollview居中显示子视图
- 对象序列化到本地文件 ObjectOutputstream ObjcetInputstream
- 对象序列化到本地文件 ObjectOutputstream ObjcetInputstream
- struts2中的constant介绍之struts.objectFactory与spring的整合
- Swift学习: 从Objective-C到Swift