org.codehaus.jackson.map.JsonMappingException:找不到合适的类型构造函数

时间:2012-08-29 16:57:07

标签: android json rest parcelable

我正在为我的AndroidApp编写RESTful客户端。我的休息webservice返回给我一个json,我用springfreamwork将它传递给java类成员。这样我的代码就可以了。 我需要将参数从main活动传递到另一个,所以我按照指南行将该类(Clinica.class见下文)实现为PARCELABLE。现在app给我发回了这个错误

    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: libcore.net.http.ChunkedInputStream@41346758; 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: libcore.net.http.ChunkedInputStream@41346758; 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);

有什么建议吗?提前谢谢。

1 个答案:

答案 0 :(得分:3)

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