1

I have a CookingEvent.class which is a subclass of Event.class and hibernate inheritance strategy is @Inheritance(strategy=InheritanceType.JOINED) . when i am trying to send a List objects as a get response .I am getting below Exception

2016-08-25 11:49:22.351 ERROR 11944 --- [nio-8189-exec-1] o.a.c.c.C.[.[.[/].[servletContainer] : Servlet.service() for servlet [servletContainer] in context with path [] threw exception [org.glassfish.jersey.server.ContainerException: com.fasterxml.jackson.databind.JsonMappingException: object is not an instance of declaring class (through reference chain: java.util.ArrayList[0]->Object[]["eventId"])] with root cause

java.lang.IllegalArgumentException: object is not an instance of declaring class at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.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) at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:506) at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:644) at 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 

        }

I have a Jersey controller as below which returns a 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);

        } 

I am using Spring Boot + Jersey + hibernate

1 Answer 1

1

Issue is resolved .

Added

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include =As.PROPERTY, property = "data")
@JsonSubTypes({ @Type(value = CookingEvent.class, name = "cookingEvent"), @Type(value = CookingRequestEvent.class, name = "cookingRequest") })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.