title: Trying to send list of class object as response to REST API
body: <p>I am trying to send List of my NotificationEntity class objects as a response to REST API call.
  What is happening is when the call is made, a query will run through database which will return a List of NotificationEntity class object(s) and when I am trying to send the List as JSON, I am getting error as </p>
  
  <blockquote>
    <p>Ljava.lang.Object; cannot be cast</p>
  </blockquote>
  
  <p>full stack trace is given below with my code.</p>
  
  <p>My controller is:</p>
  
  <pre><code>@RequestMapping(value = "/reconciler/{user}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
      ResponseEntity getUnProcessedNotifications(@PathVariable(value = "user") String user) {
           if(user.equals(auth)) {
                List&lt;String&gt; response = getPendingNotifications();
                return new ResponseEntity(response, HttpStatus.ACCEPTED);
           }
           else
                return new ResponseEntity("", HttpStatus.UNAUTHORIZED);
      }
  
  public List&lt;String&gt; getPendingNotifications() {
          List&lt;NotificationEntity&gt; notificationInstanceList = notificationInstanceRepo.getPendingMessages(queued);
          NotificationEntityWrapper notificationEntityWrapper = new NotificationEntityWrapper();
          notificationEntityWrapper.setNotificationEntityList(notificationInstanceList);
          return changeIntoJsonFormat(notificationEntityWrapper);
      }
  
      public List&lt;String&gt; changeIntoJsonFormat(NotificationEntityWrapper notificationEntityWrapper) {
          List&lt;String&gt; stringList = new ArrayList&lt;String&gt;();
          for(NotificationEntity notificationEntity: notificationEntityWrapper.getNotificationEntityList())
              stringList.add(notificationEntity.toString());
          return stringList;
      }
  </code></pre>
  
  <p>NotificationInstanceRepo interface:</p>
  
  <pre><code>public interface NotificationInstanceRepo extends JpaRepository&lt;NotificationInstance, Long&gt;
  {
  
      @Query("Select n.notificationId, n.notificationText, n.targetType, n.target, n.status, n.createdAt, n.updatedAt, n.retries, n.subject from NotificationInstance n where n.status= :queue")
      List&lt;NotificationEntity&gt; getPendingMessages(@Param("queue") String status);
  }
  </code></pre>
  
  <p>NotificationEntity POJO is:</p>
  
  <pre><code>public class NotificationEntity extends AbstractNotificationEntity {
  
      long notificationId;
      String notificationText;
      String targetType;
      String target;
      String status;
      int retries;
      String subject;
      Date createdAt;
      Date updatedAt;
      //getters and setters excluded
  
      @Override
      public String toString() {
          return "NotificationEntity [ " +
                  " notificationId=" + getNotificationId() + "," +
                  " notificationText=" + getNotificationText() + "," +
                  " targetType:" + getTargetType() + "," +
                  " target:" + getTarget() + "," +
                  " status:" + getStatus() + "," +
                  " subject:" + getSubject() + "," +
                  " createdAt:" + getCreatedAt() + "," +
                  " updatedAt:" + getUpdatedAt() + "," +
                  " retries:" + getRetries()  +
                  "]";
      }
  }
  </code></pre>
  
  <p>POJO wrapper:</p>
  
  <pre><code>public class NotificationEntityWrapper {
      private List&lt;NotificationEntity&gt; notificationEntityList;
  
      public List&lt;NotificationEntity&gt; getNotificationEntityList() {
          return notificationEntityList;
      }
  
      public void setNotificationEntityList(List&lt;NotificationEntity&gt; notificationEntityList) {
          this.notificationEntityList = notificationEntityList;
      }
  }
  </code></pre>
  
  <p>Stack trace: </p>
  
  <pre><code>ERROR 14 Mar 2016 21:34:02,099 [qtp19467337-19] com.analytics.reporting.common.commons.web.AbstractBaseController: Error generated during execution.
  java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.alps.common.model.NotificationEntity
      at com.alps.services.DispatcherServiceImpl.changeIntoJsonFormat(DispatcherServiceImpl.java:152)
      at com.alps.services.DispatcherServiceImpl.getPendingNotifications(DispatcherServiceImpl.java:147)
      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 org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
      at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
      at com.sun.proxy.$Proxy88.getPendingNotifications(Unknown Source)
      at com.alps.web.NotificationController.getUnProcessedNotifications(NotificationController.java:57)
      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 org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
      at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:812)
      at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1669)
      at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:224)
      at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
      at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration$ApplicationContextHeaderFilter.doFilterInternal(EndpointWebMvcAutoConfiguration.java:242)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
      at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:111)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
      at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
      at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:87)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
      at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
      at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
      at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:103)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
      at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585)
      at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
      at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577)
      at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
      at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
      at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
      at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
      at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
      at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
      at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
      at org.eclipse.jetty.server.Server.handle(Server.java:499)
      at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311)
      at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
      at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:544)
      at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
      at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>