org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class models.Job] from JSON String

后端 未结 2 1517
闹比i
闹比i 2021-01-13 04:13

i use the playframework and tried to deserialize some json into a java object. It worked fine, exept the relationship in the model. I got the following exception

相关标签:
2条回答
  • 2021-01-13 04:52

    Either you change your JSON in order to describe your "job" entity :

    {
       "name":"asd",
       "filepath":"blob",
       "contenttype":"image/png",
       "description":"asd",
       "job":{
          "id":"1",
           "foo", "bar"
       }
    }
    

    or you create a constructor with a String parameter in your Job bean:

    public Job(String id) {
    // populate your job with its id
    }
    
    0 讨论(0)
  • 2021-01-13 05:05

    when limited time +ee: +jax-rs && +persistence, +gson; I have solved it then as:

    @Entity
    @XmlRootElement
    @Table(name="element")
    public class Element implements Serializable {
        public Element(String stringJSON){
            Gson g = new Gson();
            Element a = g.fromJson(stringJSON, this.getClass());
            this.setId(a.getId());
            this.setProperty(a.getProperty());
        }
    
        public Element() {}
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private Integer id;
        ...
    }
    
    0 讨论(0)
提交回复
热议问题