使用RestTemplate从JSON填充对象失败 [英] Fail filling object from JSON with RestTemplate

查看:196
本文介绍了使用RestTemplate从JSON填充对象失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用websecrpits,并且想从JSON创建一个对象.我使用RestTemplate来做到这一点:

I work with websecrpits and I want to create an object from my JSON. I use RestTemplate to do that :

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

但是问题是我遇到了这个错误:

but the problem is that I got this error :

 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: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@756de23f; 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: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@756de23f; 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如下:

And the JSON that I'm trying to parse is the following :

{"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类中添加构造函数,但仍然遇到相同的错误.有人需要帮助吗?

What's wrong with my class ? it says that no suitable constructor for type Item. I tried to add a constructor the Item class but I still get the same Error. Any one to Help ?

推荐答案

谢谢大家,我在此问题中找到了答案.

thank you everyone,I hava found answer in this questuin.Fail filling object from JSON with RestTemplate

Found It !!. The solution is quite strange actually. The problem comes from the inner classes. The old structure was like this :


public class MetaData{
  ...
  public class Data {

     ....

     public class Item{
       ...
     }
   }
}
The inner classes has to be static because there is no way Jackson can instantiate directly an inner class. For more details go HERE. So what I did is:

public class MetaData{
  ...
  static class Data {

     ....

     static class Item{
       ...
     }
   }
}
Everything worked just fine. Hope that helped !

shareedit
answered Jul 9 '14 at 10:04

deltascience
1,12312048
add a comment
ok

这篇关于使用RestTemplate从JSON填充对象失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆