Object Mapper 의 사용 및 상용 주석 여과 조건

4358 단어 json 학습 총화
주로 설명 하 는 것 은 실체 서열 화 입 니 다.
실체 적
@ JSONIgnoreProperties ({"sex", "addTime"}) / / 직렬 화 할 때 무시 하 는 속성 이름 집합, 로 딩 클래스 에 제 시 된 속성 은 직렬 화 되 지 않 습 니 다.
@ JSonProperty ("stu id") / / 직렬 화 할 때 특정 속성의 이름 을 수정 하려 면 사용 할 수 있 습 니 다. 직렬 화 는 id 를 stu 로 변경 합 니 다.id
@ JSonIgnore / / 직렬 화 시 필드 를 무시 하고 속성, 방법 을 불 러 올 수 있 습 니 다.
 
Object Mapper 에서
/ / 직렬 화 시 속성 값 이 null 인 무시
MAPPER.setSerializationInclusion(Inclusion.NON_NULL);
        //이 방법 을 통 해 mapper 대상 을 설정 하고 모든 직렬 화 대상 은 개 규칙 에 따라 계열 화 됩 니 다.
        //Include. Include. 항상 기본 값
        //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 문자열 에 우리 가 필요 하지 않 은 필드 가 포함 되 어 있다 면 해당 하 는 실체 클래스 에 이 필드 가 포함 되 어 있 지 않 을 때 이상 을 던 져 실체 클래스 에서 찾 지 못 한 필드 가 있 음 을 알려 줍 니 다.해결 방법 은 매우 간단 합 니 다. Object Mapper 를 성명 한 후에 상기 코드 를 추가 합 니 다.
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
     Java.lang.Object         java.lang.Enum             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.

좋은 웹페이지 즐겨찾기