ObjectMapper 的使用和常用注解 过滤条件
2016-08-31 17:44
645 查看
主要讲解的是实体序列化是的几个注解
实体上
@JsonIgnoreProperties({"sex","addTime"}) //序列化时忽略的属性名称集合 ,加载类上,给出的属性都不序列化
@JsonProperty("stu_id") //序列化时,如果要修改某些属性的名字,可以使用, 序列化 将id改为 stu_id
@JsonIgnore //序列化时忽略字段,可以加载 属性上 、方法上
ObjectMapper上
// 序列化时 ,属性值为null的忽略
MAPPER.setSerializationInclusion(Inclusion.NON_NULL);
//通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化
//Include.Include.ALWAYS 默认
//Include.NON_DEFAULT 属性为默认值不序列化
//Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化
//Include.NON_NULL 属性为NULL 不序列化
只对pojo起作用,对map和list不起作用
忽略实体类中不含有的字段方法,用在方法中
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
下面是例子
实体类pojo
测试类
我项目中 数据库获取时只获取有用的数据,其他不获取,然后序列化
有时候,如果数据库表中增加一个字段,但返回的JSON字符串中含有我们并不需要的字段,那么当对应的实体类中不含有该字段时,会抛出一个异常,告诉你有些字段没有在实体类中找到。解决办法很简单,在声明ObjectMapper之后,加上上述代码:
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Java.lang.Object
java.lang.Enum<DeserializationFeature>
com.fasterxml.jackson.databind.DeserializationFeature
Feature that determines whether encountering of unknown properties (ones that do not map to a property,
and there is no "any setter" or handler that can handle it) should result in a failure (by throwing a
or not.
实体上
@JsonIgnoreProperties({"sex","addTime"}) //序列化时忽略的属性名称集合 ,加载类上,给出的属性都不序列化
@JsonProperty("stu_id") //序列化时,如果要修改某些属性的名字,可以使用, 序列化 将id改为 stu_id
@JsonIgnore //序列化时忽略字段,可以加载 属性上 、方法上
ObjectMapper上
// 序列化时 ,属性值为null的忽略
MAPPER.setSerializationInclusion(Inclusion.NON_NULL);
//通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化
//Include.Include.ALWAYS 默认
//Include.NON_DEFAULT 属性为默认值不序列化
//Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化
//Include.NON_NULL 属性为NULL 不序列化
只对pojo起作用,对map和list不起作用
忽略实体类中不含有的字段方法,用在方法中
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
下面是例子
实体类pojo
package cn.com.log.demo; import java.io.Serializable; import java.util.Date; import org.codehaus.jackson.annotate.JsonIgnore; import org.codehaus.jackson.annotate.JsonIgnoreProperties; import org.codehaus.jackson.annotate.JsonProperty; @JsonIgnoreProperties({"sex","addTime"}) //序列化时忽略的属性名称集合 ,加载类上,给出的属性都不序列化 public class student implements Serializable{ /** * @Fields serialVersionUID : */ private static final long serialVersionUID = -6510230638877789163L; @JsonProperty("stu_id") //序列化时,如果要修改某些属性的名字,可以使用, 序列化 将id改为 stu_id private Integer id ; @JsonIgnore //序列化时忽略字段,可以加载 属性上 、方法上 private String name ; private Integer age ; private String sex; private Date addTime ; public Date getAddTime() { return addTime; } public void setAddTime(Date addTime) { this.addTime = addTime; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
测试类
package cn.com.log.demo; import java.io.IOException; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion; public class testjson { private static final ObjectMapper MAPPER = new ObjectMapper(); public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException { // 序列化时 ,属性值为null的忽略 MAPPER.setSerializationInclusion(Inclusion.NON_NULL); /* 2.代码上 ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(Include.NON_NULL); //通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化 //Include.Include.ALWAYS 默认 //Include.NON_DEFAULT 属性为默认值不序列化 //Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化 //Include.NON_NULL 属性为NULL 不序列化 User user = new User(1,"",null); String outJson = mapper.writeValueAsString(user); System.out.println(outJson); 注意:只对VO起作用,Map List不起作用 */ student stu = new student(); stu.setName("aaav"); String data = MAPPER.writeValueAsString(stu); System.out.println(data); } }
我项目中 数据库获取时只获取有用的数据,其他不获取,然后序列化
有时候,如果数据库表中增加一个字段,但返回的JSON字符串中含有我们并不需要的字段,那么当对应的实体类中不含有该字段时,会抛出一个异常,告诉你有些字段没有在实体类中找到。解决办法很简单,在声明ObjectMapper之后,加上上述代码:
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Java.lang.Object
java.lang.Enum<DeserializationFeature>
com.fasterxml.jackson.databind.DeserializationFeature
FAIL_ON_UNKNOWN_PROPERTIES
Feature that determines whether encountering of unknown properties (ones that do not map to a property,
and there is no "any setter" or handler that can handle it) should result in a failure (by throwing a
JsonMappingException)
or not.
相关文章推荐
- mac系统使用wireshark抓取无线网卡数据包以及常用过滤条件
- Linux 使用grep筛选多个条件及grep常用过滤命令
- 同步复制怎么使用属性条件过滤创建复本
- Shell 中判断条件的使用(本文只列出并常用的)
- Python数组条件过滤filter函数使用示例
- 如何在REST API中使用查阅项的值作为过滤条件
- hibernate使用@where实现条件过滤功能
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(六): 一些常用的数据库 注解
- Spark1.3使用外部数据源时条件过滤只要是字符串类型的值均报错
- 使用ObjectMapper把json转换为实体-时间转换问题
- 常用注解的使用
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(六): 一些常用的数据库 注解
- 【WireShark】常用过滤条件
- 即时报表中使用专项过滤条件
- jackson框架的使用-annotation(注解)、属性的过滤
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(六): 一些常用的数据库 注解
- Junit框架使用(4)--JUnit常用断言及注解
- 根据条件,过滤符合条件的文件(使用了一个开源了ant.jar包)
- Spring自动注解装配、自动检测装配Bean配合过滤组件使用