2016-07-28 3 views
1

API 호출에서 반환 된 결과를 deserialize하려고합니다. 그러나 결과에는 부울 또는 배열이 포함될 수 있습니다. 결과는 I가 관련된 값을 판독하려는 Jackson 부울 및 배열 데이터 바인딩

{ 
    "succeeded": { 
    "current_page": 1, 
    "per_page": 100, 
    "results": [ 
    { 
     "get_info": { 
     "fieldA": "4198126", 
     "fieldB": "2016-05-25T22:43:52Z", 
     "fieldC": "iws-user-cfg-proxy-beta", 
     "updated_at": "2016-05-25T22:43:52Z" 
     } 
    }, 
    { 
     "get_info": { 
     "fieldA": "4551542", 
     "fieldB": "2016-07-27T22:26:27Z", 
     "fieldC": "silkRoot", 
     "updated_at": "2016-07-27T22:26:27Z" 
     } 
    } 
    ] 
}, 
"version": 1.0 
} 

처럼 배열 응답 JSON 수신 JSON 보이는 경우

결과가 부울 인 경우는, 응답 수신 JSON은

{ 
    "succeeded": true, 
    "version": 1.0 
} 

같다 성공한 필드. 매핑 클래스에서 이것을 처리 할 수있는 방법이 있습니까? 나의 현재 매핑 클래스는 다음과 같습니다 : I 프로그램에게 그것이 방법을 실행하면

public class ServResp { 

public final static String TYPE1_EXCEPTION = "Type1Exception"; 
public final static String TYPE2_EXCEPTION = "Type2Exception"; 

public final int httpStatusCode; 
public final boolan succeeded; 
public final String version; 
public final String exception; 
public final String exceptionMessage; 

private ServResp(Builder builder) { 
    this.httpStatusCode = builder.httpStatusCode; 
    this.succeeded = builder.succeeded; 
    this.version = builder.version; 
    this.exception = builder.exception; 
    this.exceptionMessage = builder.exceptionMessage; 
} 

public Builder modify() { 
    return new Builder(this); 
} 

@Override 
public int hashCode() { 
    final int prime = 31; 
    int result = 1; 
    result = prime * result + ((exception == null) ? 0 : exception.hashCode()); 
    result = prime * result + ((exceptionMessage == null) ? 0 : exceptionMessage.hashCode()); 
    result = prime * result + httpStatusCode; 
    result = prime * result + (succeeded ? 17 : 19); 
    result = prime * result + ((version == null) ? 0 : version.hashCode()); 
    return result; 
} 

@Override 
public boolean equals(Object obj) { 
    if (this == obj) 
     return true; 
    if (obj == null) 
     return false; 
    if (getClass() != obj.getClass()) 
     return false; 
    ServResp other = (ServResp) obj; 
    if (exception == null) { 
     if (other.exception != null) 
      return false; 
    } else if (!exception.equals(other.exception)) 
     return false; 
    if (exceptionMessage == null) { 
     if (other.exceptionMessage != null) 
      return false; 
    } else if (!exceptionMessage.equals(other.exceptionMessage)) 
     return false; 
    if (httpStatusCode != other.httpStatusCode) 
     return false; 
    if (succeeded != other.succeeded) 
     return false; 
    if (version == null) { 
     if (other.version != null) 
      return false; 
    } else if (!version.equals(other.version)) 
     return false; 

    return true; 
} 

public static class Builder { 

    private int httpStatusCode; 
    private boolean succeeded; 
    private String version; 
    private String exception; 
    private String exceptionMessage; 

    public Builder() { 
    } 

    public Builder(ServResp other) { 
     this.httpStatusCode = other.httpStatusCode; 
     this.version = other.version; 
     this.exception = other.exception; 
     this.exceptionMessage = other.exceptionMessage; 
    } 

    public Builder setHttpStatusCode(int httpStatusCode) { 
     this.httpStatusCode = httpStatusCode; 
     return this; 
    } 

    public Builder setSucceeded(boolean succeeded) { 
     this.succeeded = succeeded; 
     return this; 
    } 

    public Builder setVersion(String version) { 
     this.version = version; 
     return this; 
    } 

    public Builder setException(String exception) { 
     this.exception = exception; 
     return this; 
    } 

    public Builder setExceptionMessage(String exceptionMessage) { 
     this.exceptionMessage = exceptionMessage; 
     return this; 
    } 

    public ServResp build() { 
     return new ServResp(this); 
    } 
}} 

, 나는 오류가 아래 얻을 :

Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.boolean out of START_OBJECT token 

이 주위에 얻을 수있는 방법이 있나요?

감사

답변

1

당신은 ObjectBuilder.succeeded의 유형을 변경하려고하고 나중에 읽기 위해 몇 가지 코드를 추가 할 수 있습니다. 이것은 미래 버그의 소스처럼 들리지만 API를 제어하지 않으면 가장 좋은 기회 일 수 있습니다. 반면에, 당신은 API를 제어 할, 경우

public class Foo { 
     private Object overRiddenJsonType; 

     public Object getOverRiddenJsonType() { 
      return overRiddenJsonType; 
     } 

     public void setOverRiddenJsonType(Object overRiddenJsonType) { 
      this.overRiddenJsonType = overRiddenJsonType; 
     } 
    } 

    public class FooConsumer { 
     public void consumeFoo(Foo foo) { 
      Boolean b = false; 
      Bar bar = null; 
      if (foo.getOverRiddenJsonType() instanceof Boolean) { 
       b = (Boolean)foo.getOverRiddenJsonType(); 
       // worry about NPE from unboxing later... 
      } else if (foo.getOverRiddenJsonType() instanceof Bar) { 
       bar = (Bar)foo.getOverRiddenJsonType(); 
      } 
      // ... 
     } 

}

, 다음 더 나은 솔루션은 다시 구조를 위해 success 항상 boolean되도록 JSON, 데이터의 나머지 부분을 것 최상위 필드 또는 results의 멤버 중 하나입니다 :

{ 
     "succeeded": true, 
     "version": 1.0, 
     "current_page": 1, 
     "per_page": 100, 
     "results": [ 
     { 
      "get_info": { 
      "fieldA": "4198126", 
      ... 
      } 
     ] 
    } 
+0

API를 제어 할 수 있습니다 ... 두 번째 접근 방법이 훨씬 더 좋습니다 ... 시도해 볼 것입니다 ... 감사합니다. – learningMyWayThru

1

나는이 도구 jsonschematopojo를 사용하여 아래의 JSON에 대한 일반 POJO를 생성하는 것이 좋습니다 것입니다. pojo를 생성하는 동안 Source type as JSON과 Annotation style : none을 선택할 수 있습니다. 당신이 당신의 프로젝트에 생성 된 빈을 추가하면

{ 
    "succeeded": { 
    "current_page": 1, 
    "per_page": 100, 
    "results": [ 
    { 
     "get_info": { 
     "fieldA": "4198126", 
     "fieldB": "2016-05-25T22:43:52Z", 
     "fieldC": "iws-user-cfg-proxy-beta", 
     "updated_at": "2016-05-25T22:43:52Z" 
     } 
    }, 
    { 
     "get_info": { 
     "fieldA": "4551542", 
     "fieldB": "2016-07-27T22:26:27Z", 
     "fieldC": "silkRoot", 
     "updated_at": "2016-07-27T22:26:27Z" 
     } 
    } 
    ] 
} 
} 

, 당신은이 방법을 작동하는 경우

private Succeeded succeeded; 

/** 
* 
* @return 
*  The succeeded 
*/ 
public Succeeded getSucceeded() { 
    return succeeded; 
} 

/** 
* 
* @param succeeded 
*  The succeeded 
*/ 
public void setSucceeded(Succeeded succeeded) { 
    this.succeeded = succeeded; 
} 

가 확인하시기 바랍니다 귀하의 매퍼 클래스에서이 오버로드 된 메서드를 추가 할 수 있습니다.

+0

제안 해 주셔서 감사합니다. 이 도구는 json을 기준으로 사용할 수있는 POJO 객체로 변환하는 데 유용합니다. – learningMyWayThru