Tobia
Tobia

Reputation: 9516

JsonMappingException during Json unmarshal in Spring

This is my POJO class:

public class OrdineIngressi {

    private Integer spettacolo;
    private Integer settore;
    private Integer pv;
    private List<OrdineIngresso> ingressi=new ArrayList<OrdineIngresso>();

    public OrdineIngressi(Integer spettacolo, Integer settore, Integer pv,
            List<OrdineIngresso> ingressi) {
        super();
        this.spettacolo = spettacolo;
        this.settore = settore;
        this.pv = pv;
        this.ingressi = ingressi;
    }



    public OrdineIngressi() {
        super();
    }



    public Integer getSpettacolo() {
        return spettacolo;
    }
    public void setSpettacolo(Integer spettacolo) {
        this.spettacolo = spettacolo;
    }
    public Integer getSettore() {
        return settore;
    }
    public void setSettore(Integer settore) {
        this.settore = settore;
    }   
    public Integer getPv() {
        return pv;
    }
    public void setPv(Integer pv) {
        this.pv = pv;
    }
    public List<OrdineIngresso> getIngressi() {
        return ingressi;
    }
    public void setIngressi(List<OrdineIngresso> ingressi) {
        this.ingressi = ingressi;
    }



    public class OrdineIngresso {

        private Integer tipoingresso;
        private Integer abbonamento;
        private int numero;
        private Integer[] posti;

        public OrdineIngresso() {
            super();
        }



        public OrdineIngresso(Integer tipoingresso, Integer abbonamento,
                int numero, Integer[] posti) {
            super();
            this.tipoingresso = tipoingresso;
            this.abbonamento = abbonamento;
            this.numero = numero;
            this.posti = posti;
        }
        public Integer getTipoingresso() {
            return tipoingresso;
        }
        public void setTipoingresso(Integer tipoingresso) {
            this.tipoingresso = tipoingresso;
        }
        public Integer getAbbonamento() {
            return abbonamento;
        }
        public void setAbbonamento(Integer abbonamento) {
            this.abbonamento = abbonamento;
        }
        public Integer[] getPosti() {
            return posti;
        }
        public void setPosti(Integer[] posti) {
            this.posti = posti;
        }
        public int getNumero() {
            return numero;
        }
        public void setNumero(int numero) {
            this.numero = numero;
        }   


    }   

}

This is the ajax input:

{"spettacolo":1,"settore":1,"pv":1,"ingressi":[{"tipoingresso":1,"abbonamento":null,"numero":1,"posti":[]}]}

When the Controller tries to unmarshal a json input I got this:

nested exception is org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class com.bean.OrdineIngressi$OrdineIngresso]: can not instantiate from JSON object (need to add/enable type information?)

Why? There is a default constructor!

Upvotes: 1

Views: 196

Answers (1)

Anders R. Bystrup
Anders R. Bystrup

Reputation: 16060

You're almost there, you just need to make your inner class static:

...
    public static class OrdineIngresso {
        private Integer tipoingresso;
        ...
    }

IIRC it has to do with the fact that a non-args inner class constructor really isn't non-args and thus jackson doesn't have a general manner for instantiating these non-static inner classes.

Cheers,

Upvotes: 2

Related Questions