2014-07-09 5 views
0

websecrpits에서 작업하고 JSON에서 객체를 만들고 싶습니다. 내가 할 RestTemplate를 사용하는 :RestTemplate을 사용하여 JSON의 객체를 채우지 못함

MetaData entity = restTemplate.postForObject(url + "?alf_ticket={ticket}", requestEntity, MetaData.class, _ticket); 

하지만 문제는이 오류를 가지고 있다는 것입니다 :

Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: No suitable constructor found for type [simple type, class custom.alfresco.logic.connect.models.MetaData$Item]: can not instantiate from JSON object (need to add/enable type information?) 
    at [Source: [email protected]e23f; line: 7, column: 5] (through reference chain: custom.alfresco.logic.connect.models.MetaData["data"]->custom.alfresco.logic.connect.models.Data["items"]); nested exception is org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class custom.alfresco.logic.connect.models.MetaData$Item]: can not instantiate from JSON object (need to add/enable type information?) 
    at [Source: [email protected]e23f; line: 7, column: 5] (through reference chain: custom.alfresco.logic.connect.models.MetaData["data"]->custom.alfresco.logic.connect.models.Data["items"]) 
     at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readJavaType(MappingJacksonHttpMessageConverter.java:187) 
     at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.read(MappingJacksonHttpMessageConverter.java:179) 
     at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:95) 
     at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:549) 
     at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:502) 
     at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:330) 
     at com.custom.alfresco.templates.postTemplate.postObjectResponse(postTemplate.java:112) 
     at custom.alfresco.logic.connect.RepositoryOperation.getMetadata(RepositoryOperation.java:617) 
     at custom.alfresco.UI.main.main(main.java:35) 

내 클래스 MetaData은 다음과 같다 :

@JsonInclude(JsonInclude.Include.NON_NULL) 
@JsonPropertyOrder({ "data" }) 
public class MetaData extends CommunicationObject { 

    @JsonProperty("data") 
    private Data data; 

    @JsonIgnore 
    private Map<String, Object> additionalProperties = new HashMap<String, Object>(); 

    @JsonProperty("data") 
    public Data getData() { 
    return data; 
    } 

    @JsonProperty("data") 
    public void setData(Data data) { 
    this.data = data; 
    } 

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

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

    @JsonInclude(JsonInclude.Include.NON_NULL) 
    @JsonPropertyOrder({ "items" }) 
    public class Data { 

    @JsonProperty("items") 
    private List<Item> items = new ArrayList<Item>(); 

    @JsonIgnore 
    private Map<String, Object> additionalProperties = new HashMap<String, Object>(); 

    @JsonProperty("items") 
    public List<Item> getItems() { 
     return items; 
    } 

    @JsonProperty("items") 
    public void setItems(List<Item> items) { 
     this.items = items; 
    } 

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

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

    } 

    @JsonInclude(JsonInclude.Include.NON_NULL) 
    @JsonPropertyOrder({ "type", "parentType", "isContainer", "name", "title", "description", "modified", "modifier", 
     "displayPath", "nodeRef" }) 
    public class Item { 

    @JsonProperty("type") 
    private String type; 

    @JsonProperty("parentType") 
    private String parentType; 

    @JsonProperty("isContainer") 
    private Boolean isContainer; 

    @JsonProperty("name") 
    private String name; 

    @JsonProperty("title") 
    private String title; 

    @JsonProperty("description") 
    private String description; 

    @JsonProperty("modified") 
    private String modified; 

    @JsonProperty("modifier") 
    private String modifier; 

    @JsonProperty("displayPath") 
    private String displayPath; 

    @JsonProperty("nodeRef") 
    private String nodeRef; 

    @JsonIgnore 
    private Map<String, Object> additionalProperties = new HashMap<String, Object>(); 

    @JsonProperty("type") 
    public String getType() { 
     return type; 
    } 

    @JsonProperty("type") 
    public void setType(String type) { 
     this.type = type; 
    } 

    @JsonProperty("parentType") 
    public String getParentType() { 
     return parentType; 
    } 

    @JsonProperty("parentType") 
    public void setParentType(String parentType) { 
     this.parentType = parentType; 
    } 

    @JsonProperty("isContainer") 
    public Boolean getIsContainer() { 
     return isContainer; 
    } 

    @JsonProperty("isContainer") 
    public void setIsContainer(Boolean isContainer) { 
     this.isContainer = isContainer; 
    } 

    @JsonProperty("name") 
    public String getName() { 
     return name; 
    } 

    @JsonProperty("name") 
    public void setName(String name) { 
     this.name = name; 
    } 

    @JsonProperty("title") 
    public String getTitle() { 
     return title; 
    } 

    @JsonProperty("title") 
    public void setTitle(String title) { 
     this.title = title; 
    } 

    @JsonProperty("description") 
    public String getDescription() { 
     return description; 
    } 

    @JsonProperty("description") 
    public void setDescription(String description) { 
     this.description = description; 
    } 

    @JsonProperty("modified") 
    public String getModified() { 
     return modified; 
    } 

    @JsonProperty("modified") 
    public void setModified(String modified) { 
     this.modified = modified; 
    } 

    @JsonProperty("modifier") 
    public String getModifier() { 
     return modifier; 
    } 

    @JsonProperty("modifier") 
    public void setModifier(String modifier) { 
     this.modifier = modifier; 
    } 

    @JsonProperty("displayPath") 
    public String getDisplayPath() { 
     return displayPath; 
    } 

    @JsonProperty("displayPath") 
    public void setDisplayPath(String displayPath) { 
     this.displayPath = displayPath; 
    } 

    @JsonProperty("nodeRef") 
    public String getNodeRef() { 
     return nodeRef; 
    } 

    @JsonProperty("nodeRef") 
    public void setNodeRef(String nodeRef) { 
     this.nodeRef = nodeRef; 
    } 

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

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

    } 

} 

그리고 JSON 그 구문 분석하려고하면 다음과 같습니다 :

{"data": 
{ 
    "items": 
    [ 
     { 
      "type": "cm:content", 
      "parentType": "cm:cmobject", 
      "isContainer": false, 
      "name": "second.alf", 
      "title": "", 
      "description": "", 
      "modified": "2014-07-01T16:21:10.712+02:00", 
      "modifier": "admin", 

      "displayPath": "\/Espace racine\/Alfredine\/Custom saved documents", 
      "nodeRef": "workspace://SpacesStore/6f8ebd09-f7bf-4967-9fc5-a90a9d825088" 
     } 
    ] 
} 
} 

수업 시간에 문제가 있습니까? Item 유형에 적합한 생성자가 없다고 말합니다. Item 클래스의 생성자를 추가하려고했지만 여전히 동일한 오류가 발생합니다. 도움을 줄 사람이 있습니까?

답변

0

발견 It !! 해결책은 실제로 이상합니다. 문제는 내부 클래스에서 비롯됩니다. 오래된 구조는 다음과 같았다 :

public class MetaData{ 
    ... 
    public class Data { 

    .... 

    public class Item{ 
     ... 
    } 
    } 
} 

내부 클래스는 직접 내부 클래스를 인스턴스화 할 수있는 방법 Jackson이 없기 때문에 정적한다. 자세한 내용은 HERE을 참조하십시오. 그래서 내가 한 일은 다음과 같습니다.

public class MetaData{ 
    ... 
    static class Data { 

    .... 

    static class Item{ 
     ... 
    } 
    } 
} 

모든 것이 잘 작동했습니다. 희망이 도움이!

관련 문제