com.fasterxml.jackson.databind.JsonMappingException:object不是声明类的实例

时间:2016-08-25 06:39:38

标签: json hibernate jersey jackson

我有一个CookingEvent.class,它是Event.class的子类,而hibernate继承策略是@Inheritance(strategy = InheritanceType.JOINED)。当我尝试将List对象作为get响应发送时。我的异常

  

2016-08-25 11:49:22.351 ERROR 11944 --- [nio-8189-exec-1]   o.a.c.c.C。[。[。[/]。[servletContainer]:Servlet.service()for   与path []的上下文中的servlet [servletContainer]引发了异常   [org.glassfish.jersey.server.ContainerException:   com.fasterxml.jackson.databind.JsonMappingException:object不是   声明类的实例(通过引用链:   具有根本原因的java.util.ArrayList [0] - > Object [] [“eventId”])]

     

java.lang.IllegalArgumentException:object不是。的实例   宣布上课   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:497)at   com.fasterxml.jackson.databind.ser.BeanPropertyWriter.get(BeanPropertyWriter.java:726)     在   com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:506)     在   com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:644)     在   com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)

@Entity
@Table(name = "users")
@JsonAutoDetect
public class Users  implements java.io.Serializable {

  @JsonBackReference
    private List<Event> eventByUser = new ArrayList<Event>(
            0);

    @Id
    @GeneratedValue
    @Column(name = "USER_ID", unique = true)
    public Long getUserId() {
        return userIds;
    }
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
    @Fetch(FetchMode.JOIN)
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    @JsonIgnore
    public List<Event> getEventByUser() {
        return eventByUser;
    }
}


  @Entity
    @Table(name = "EVENT")
    @Inheritance(strategy=InheritanceType.JOINED)
    public class Event  implements java.io.Serializable {

        private Integer eventId;

        @Id
        @GeneratedValue
        @Column(name = "COOKING_EVENT_ID", unique = true)
        public Integer getEventId() {
            return eventId;
        }
        private Users user;

        @ManyToOne(fetch = FetchType.LAZY)
    @Fetch(FetchMode.SELECT)
    @JsonIgnore
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    @JoinColumn(name = "ASSIGNED_USER_ID", nullable = false)
    @JsonManagedReference
    public Users getUser() {
        return user;
    }

        public void setEventId(Integer eventId) {
            this.eventId = eventId;
        }



 @Entity
    @Table(name = "COOKING_REQUEST_EVENT") @PrimaryKeyJoinColumn(name="EVENT_ID") 
public class CookingRequestEvent extends Event implements java.io.Serializable {

        //Other Variables along with setters and getters 

        }

我有一个Jersey控制器,如下所示返回一个List

  @GET
        @Path("/cookingEventsByUser/{userId}")
        @Produces({ MediaType.APPLICATION_JSON})
        public List<CookingEvent>  getEventsById(@PathParam("userId") Long id)
                throws JsonGenerationException, JsonMappingException, IOException {
          List<CookingEvent> events  = new ArrayList<CookingEvent>();
         events =  cookingEventServices.getCookingEventsByUser(id);

        } 

我正在使用Spring Boot + Jersey + hibernate

1 个答案:

答案 0 :(得分:0)

问题已解决。

已添加

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include =As.PROPERTY, property = "data")
@JsonSubTypes({ @Type(value = CookingEvent.class, name = "cookingEvent"), @Type(value = CookingRequestEvent.class, name = "cookingRequest") })