2012-08-29 54 views
0

我正在为我的AndroidApp编写一个RESTful客户端。我的休息webservice返回给我一个JSON,我用springfreamwork将它分解成java类成员。这样我的代码就可以了。 我需要将参数从主要活动传递到另一个参与者,所以我按照指导原则将该类(Clinica.class见下文)作为PARCELABLE实现。现在,应用程序将返回我这个错误org.codehaus.jackson.map.JsonMappingException:没有找到合适的构造函数类型

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: No suitable constructor found for type [simple type, class it.stasbranger.clinigomobile.model.Clinica]: can not instantiate from JSON object (need to add/enable type information?) 
at [Source: [email protected]; line: 1, column: 3]; nested exception is org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class it.stasbranger.clinigomobile.model.Clinica]: can not instantiate from JSON object (need to add/enable type information?) 
at [Source: [email protected]; line: 1, column: 3] 

这是我Clinica.class

public class Clinica implements Parcelable { 

     @JsonProperty 
     private Integer idclinica; 
     @JsonProperty 
     private String nome; 
     @JsonProperty 
     private Long dataRegistrazione; 
     @JsonProperty 
     private Long version; 
     @JsonProperty 
     private String referente; 
     @JsonProperty 
     private String indirizzo; 
     @JsonProperty 
     private String cap; 
     @JsonProperty 
     private String telefono; 
     @JsonProperty 
     private String email; 
     @JsonProperty 
     private String sitoWeb; 
     @JsonProperty 
     private Boolean abilitata; 
     @JsonProperty 
     private Integer valutazione; 
     @JsonProperty 
     private Double rank; 
     @JsonProperty 
     private String nomeFatturazione; 

     //getters and setters 
      ....... 

     public Clinica (Parcel p){ 
      boolean[] booleans = new boolean[1]; 

      this.cap=p.readString(); 
      this.email=p.readString(); 
      this.indirizzo=p.readString(); 
      this.nome=p.readString(); 
      this.nomeFatturazione=p.readString(); 
      this.referente=p.readString(); 
      this.sitoWeb=p.readString(); 
      this.telefono=p.readString(); 

      this.idclinica=p.readInt(); 
      this.valutazione=p.readInt(); 

      this.dataRegistrazione=p.readLong(); 
      this.version=p.readLong(); 

      this.rank=p.readDouble(); 

      p.readBooleanArray(booleans); 
      this.abilitata=booleans[0]; 
     } 

     public int describeContents() { 
      return 0; 
     } 

     public void writeToParcel(Parcel dest, int flags) { 
      boolean[] booleans = new boolean[1]; 
      Arrays.fill(booleans, abilitata); 
      dest.writeString(cap); 
      dest.writeString(email); 
      dest.writeString(indirizzo); 
      dest.writeString(nome); 
      dest.writeString(nomeFatturazione); 
      dest.writeString(referente); 
      dest.writeString(sitoWeb); 
      dest.writeString(telefono); 
      dest.writeInt(idclinica); 
      dest.writeInt(valutazione); 
      dest.writeLong(dataRegistrazione); 
      dest.writeLong(version); 
      dest.writeDouble(rank); 
      dest.writeBooleanArray(booleans); 
     } 

     public static final Parcelable.Creator<Clinica> CREATOR = new Creator<Clinica>() { 

      public Clinica[] newArray(int size) { 
       return new Clinica[size]; 
      } 

      public Clinica createFromParcel(Parcel source) { 
       return new Clinica(source); 
      } 
     }; 

    } 

,这是我的异步调用,使请求

...... 
Clinica data[] = restTemplate.getForObject(urls[0], Clinica[].class, vars); 

有什么建议?提前致谢。

回答

3

答案很简单:删除构造函数(同样在父类中,如果里面有任何东西),一切都会工作!也许有像@JsonIgnore这样的注释,但它不适用于构造函数。

+0

你能帮我知道,为什么我们需要删除参数构造函数或添加无参数的构造函数? –

+0

呃,这很长一段时间,所以我可以猜测它:jackson增加了自己的构造函数,并且由于你定义了一个构造函数,它不知道使用哪一个构造函数。看看[这篇文章](http://www.cowtowncoder.com/blog/archives/2011/07/entry_457.html)。 – shmoula

相关问题