尝试发送类对象列表作为对REST API的响应

时间:2016-03-14 16:40:25

标签: java json spring rest

我正在尝试发送我的NotificationEntity类对象列表作为对REST API调用的响应。 发生的事情是,当调用时,查询将通过数据库运行,该数据库将返回NotificationEntity类对象的List,当我尝试将List作为JSON发送时,我收到错误

  

Ljava.lang.Object;无法施展

下面用我的代码给出了完整的堆栈跟踪。

我的控制器是:

@RequestMapping(value = "/reconciler/{user}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    ResponseEntity getUnProcessedNotifications(@PathVariable(value = "user") String user) {
         if(user.equals(auth)) {
              List<String> response = getPendingNotifications();
              return new ResponseEntity(response, HttpStatus.ACCEPTED);
         }
         else
              return new ResponseEntity("", HttpStatus.UNAUTHORIZED);
    }

public List<String> getPendingNotifications() {
        List<NotificationEntity> notificationInstanceList = notificationInstanceRepo.getPendingMessages(queued);
        NotificationEntityWrapper notificationEntityWrapper = new NotificationEntityWrapper();
        notificationEntityWrapper.setNotificationEntityList(notificationInstanceList);
        return changeIntoJsonFormat(notificationEntityWrapper);
    }

    public List<String> changeIntoJsonFormat(NotificationEntityWrapper notificationEntityWrapper) {
        List<String> stringList = new ArrayList<String>();
        for(NotificationEntity notificationEntity: notificationEntityWrapper.getNotificationEntityList())
            stringList.add(notificationEntity.toString());
        return stringList;
    }

NotificationInstanceRepo界面:

public interface NotificationInstanceRepo extends JpaRepository<NotificationInstance, Long>
{

    @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<NotificationEntity> getPendingMessages(@Param("queue") String status);
}

NotificationEntity POJO是:

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()  +
                "]";
    }
}

POJO包装:

public class NotificationEntityWrapper {
    private List<NotificationEntity> notificationEntityList;

    public List<NotificationEntity> getNotificationEntityList() {
        return notificationEntityList;
    }

    public void setNotificationEntityList(List<NotificationEntity> notificationEntityList) {
        this.notificationEntityList = notificationEntityList;
    }
}

堆栈追踪:

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)

2 个答案:

答案 0 :(得分:0)

您是否尝试使用Spring的@ResponseBody ?,使用示例:

@RequestMapping(value = "/reconciler/{user}", method = RequestMethod.GET)
@ResponseBody 
List<NotificationEntity> getUnProcessedNotifications(@PathVariable(value = "user") String user) {
    ...

答案 1 :(得分:0)

尝试像这样在控制器中将@PathVariable更改为@RequestBody

@RequestMapping(value = "/reconciler/{user}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    ResponseEntity getUnProcessedNotifications(@RequestBody NotificationEntity user) {

}