2013-01-07 2 views
0

responsenull 값을 제공하지만 fiddler에서 동일한 값을 실행하면 많은 데이터가 발생합니다. 필자는 코드 작성 횟수를 줄이고 온라인 도구를 사용하여 코드를 생성했습니다. 나는 어수선한 곳을 궁금해. 응답시 null 값을 반환하는 JSON

Publications response = null ; 
    // First open URL connection (using JDK; similar with other libs) 
    try { 
     URL url = new URL(
       "http://myserver:myport/Mobile/GetPublications"); 
     HttpURLConnection connection = (HttpURLConnection)url.openConnection() ; 
     connection.setDoInput(true); 
     connection.setDoOutput(true); 
     connection.setRequestMethod("POST") ; 
     connection.setRequestProperty("Content-Type", "application/json") ; 
     // and other configuration if you want, timeouts etc 
     // then send JSON request 
     Publications request = new Publications(); // POJO with getters or public fields 
     ObjectMapper mapper = new ObjectMapper(); 
     mapper.writeValue(connection.getOutputStream(), request); 
     // and read response 
     response = mapper.readValue(
       connection.getInputStream(), Publications.class); 
    } catch (JsonGenerationException e) { 
     e.printStackTrace(); 
     fail() ; 
    } catch (JsonMappingException e) { 
     e.printStackTrace(); 
     fail() ; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     fail() ; 
    } 
    assertNotNull(response) ; 
    assertTrue(response.getPublicationInfo() != null) ; 
//  assertTrue(response.getPublicationInfo().size() > 0); 
//  assertNotNull(((PublicationInfo)response.getPublicationInfo().get(0)).getPublicationTitle() != null) ; 

그리고 간행물 POJO는

import com.fasterxml.jackson.* 

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) 
@Generated("com.googlecode.jsonschema2pojo") 
@JsonPropertyOrder({ "ErrorInfo", "PublicationInfo" }) 
public class Publications { 

@JsonProperty("ErrorInfo") 
private Object ErrorInfo; 

@JsonProperty("PublicationInfo") 
private List<PublicationInfo> PublicationInfo = new ArrayList<PublicationInfo>(); 
private Map<String, Object> additionalProperties = new HashMap<String, Object>(); 

@JsonProperty("ErrorInfo") 
public Object getErrorInfo() { 
    return ErrorInfo; 
} 

@JsonProperty("ErrorInfo") 
public void setErrorInfo(Object ErrorInfo) { 
    this.ErrorInfo = ErrorInfo; 
} 

@JsonProperty("PublicationInfo") 
public List<PublicationInfo> getPublicationInfo() { 
    return PublicationInfo; 
} 

@JsonProperty("PublicationInfo") 
public void setPublicationInfo(
     List<PublicationInfo> PublicationInfo) { 
    this.PublicationInfo = PublicationInfo; 
} 

@JsonAnyGetter 
public Map<String, Object> getAdditionalProperties() { 
    return this.additionalProperties; 
} 

@JsonAnySetter 
public void setAdditionalProperties(String name, Object value) { 
    this.additionalProperties.put(name, value); 
} 
} 

내가 assertTrue(response.getPublicationInfo().size() > 0);에서 테스트 케이스 실패를 얻을 수있다. 그러나 피들러는이

{ 
"SpecialPublications": { 
    "ErrorInfo": null, 
    "SpecialPublicationInfo": [ 
     { 
      "Date": "2/3/2010", 
      "URL": "SOME URL HERE", 
      "Description": "So much to do so less time." 
     }, 
     { 
      "Date": "2/4/2010", 
      "URL": "SOME MORE URL HERE", 
      "Description": "I need more time" 
     } 
    ] 
} 
} 
+1

먼저 JSON 구문 분석 전에 실제로 응답을 받았는지 확인하는 것이 좋습니다. –

+0

SpotlightPublicationInfo! = PublicationInfo? 후자를 어디에 매핑하고 있습니까? – Perception

답변

5

다음 당신은 모든 장소에서 @JsonProperty("SpotlightPublicationInfo")@JsonProperty("PublicationInfo")을 변경해야합니다 반환합니다. 속성 이름이 잘못되었습니다.

@JsonProperty의 의미는 json에 나타나는 속성 이름입니다.

EDIT 또한 json에서 허위 속성 이름을 제거하십시오.

response = mapper.readValue(connection.getInputStream(), Publications.class); 

으로 : 교체

JsonNode responseJson = mapper.readTree(connection.getInputStream()); 
response = mapper.readValue(responseJson.traverse().getValueAsString("SpecialPublications"), Publications.class); 

이것은 당신이 구문 분석하려고 개체의 가짜 {"SpecialPublications": ... } 래퍼를 제거합니다.

+0

이 변경 작업을 수행했지만 여전히 동일한 문제가 있습니다. junit 테스트는 통과하지 않고 바이올린은 전체 트럭에 데이터로드를 반환합니다. – taxeeta

+1

@taxeeta : 네, 거기에는 또 하나의 이유가 있습니다. json에'{ "SpecialPublications": ...}'래퍼가 없으면 코드가 작동합니다. 그것을 제거하는 가장 쉬운 방법이 무엇인지 생각해 봅시다. 정규식을 통해 항상 제거하면 옵션이 될까요? –

+1

@ taxeeta 여기 내 제안을 간다. 텍스트 편집기에서만 작성 했으므로 오류를 다시 작성하십시오. 직접 시도하지 않았습니다. –

관련 문제