flexjson에서 1:n 객체 serialization 하기.

이 앞의 mybatis에서 1:n 객체를 가져오는 것 마지막의 json 변환 부분.
무턱대고 JSONSerializer로 serializing을 하니 List 부분이 쏙 빠지더라. 이건 따로 명시를 해줘야 포함이 된다.

아래와 같이 include 함수를 이용한다.

    ItemMain itemMain = this.itemService.getItem(item_id);
    
    ResponseOne response = new ResponseOne(new Meta(Http.Status.OK, "Success"), itemMain);
    String jsonResult = new JSONSerializer()
    .prettyPrint(true)
    .include("data.itemImages")
    .exclude("*.class")
    .serialize(response);

저기서 include의 파라메터가 data.itemImages로 점 연산자가 사용된 이유는 ResponseOne 객체 안의 Object가 data로 선언이 되어 있고 거기에 매핑된 것이 itemMain이기 때문.
그리고 itemMain 내부에 itemImages 라는 이름으로 List가 매핑되어 있다.

public class ResponseOne {

 private Meta meta;
 private Object data;

 
 public ResponseOne() {
  super();
 }
 public ResponseOne(Meta meta) {
  this.meta = meta;
 }
 public ResponseOne(Meta meta, Object data) {
  super();
  this.meta = meta;
  this.data = data;
 }
 public Meta getMeta() {
  return meta;
 }
 public void setMeta(Meta meta) {
  this.meta = meta;
 }
 public Object getData() {
  return data;
 }
 public void setData(Object data) {
  this.data = data;
 }
 
}

저렇게 점 연산자를 이용해서 몇 단계로 매핑된 것을 가져올 수 있음.

댓글 없음:

댓글 쓰기