2016-08-25 167 views
1

我有一個CookingEvent.class,它是Event.class的子類,而hibernate的繼承策略是@Inheritance(strategy = InheritanceType.JOINED)。當我試圖發送一個List對象爲獲取響應。我正在逐漸低於異常com.fasterxml.jackson.databind.JsonMappingException:對象不是聲明類的實例

2016年8月25日11:49:22.351錯誤11944 --- [NIO-8189-EXEC-1] oaccC [。[。[。] [servletContainer]:Servlet.service()用於 上下文中的servlet [servletContainer]引發異常 [org.glassfish.jersey.server.ContainerException: com.fasterxml.jackson .databind.JsonMappingException:對象不是聲明類(通過參考鏈: java.util.ArrayList中[0] - >對象[] [ 「EVENTID」])的一個實例 ]與根源

java.lang.IllegalArgumentException異常:對象不是在在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.NativeMethodAccessorImpl.invoke0(本機方法) 聲明類的一個實例,在 的太陽。反射.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at 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(豆SerializerBase.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 

     } 

我有澤西控制器作爲低於該返回一個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 +引導+新澤西冬眠

回答

0

問題得到解決。

新增

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