您的位置:首页 > 移动开发 > Objective-C

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

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐