简体   繁体   English

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

[英]Trying to send list of class object as response to REST API

I am trying to send List of my NotificationEntity class objects as a response to REST API call. 我正在尝试发送我的NotificationEntity类对象的列表,作为对REST API调用的响应。 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 发生了什么事,当进行调用时,查询将遍历数据库,该数据库将返回NotificationEntity类对象的列表,并且当我尝试将列表作为JSON发送时,出现以下错误:

Ljava.lang.Object; Ljava.lang.Object; cannot be cast 不能投

full stack trace is given below with my code. 我的代码在下面给出了完整的堆栈跟踪。

My controller is: 我的控制器是:

@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 interface: 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 is: 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 wrapper: POJO包装器:

public class NotificationEntityWrapper {
    private List<NotificationEntity> notificationEntityList;

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

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

Stack trace: 堆栈跟踪:

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)

Did you try to use @ResponseBody of Spring?, an example of use: 您是否尝试过使用Spring的@ResponseBody ?,一个使用示例:

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

Try to change @PathVariable to @RequestBody in the controller like this 尝试像这样在控制器@PathVariable更改为@RequestBody

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 How to create pojo class of Object of ArrayList from REST API response? - How to create pojo class of Object of ArrayList from REST API response? 如何返回 JSONObject 列表作为 REST API 响应 - How to return a list of JSONObject as REST API response 无法将对象发送到Angular 6中的REST API - Not able to send Object to rest API in angular 6 我想将列表(它是对象的成员)从 Postman 发送到 Spring REST ZDB974238714CA8ACED638 - I want to send a List(which is a member of an object) from Postman to Spring REST API Spring boot rest api - 我可以在不为响应对象创建任何 Java 类(DTO 或实体)的情况下获得响应吗? - Spring boot rest api - Can I get response without creating any java class(DTO's or entity) for the response object? Retrofit 发送 Json object 并获取列表作为响应 - Retrofit send Json object and get List as response 尝试解析简单的REST API响应时,GSON抛出错误:预期为BEGIN_ARRAY,但在第1行第2列路径$处为BEGIN_OBJECT - GSON throwing error when trying to pares a simple rest api response: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ 将日期发送到 API 并使用 Rest 模板获得响应 - Send date to API and get a response using Rest Template 如何发送 Stream<string> 作为来自 REST API 的客户的响应?</string> - How to send Stream<String> as response to client from REST API? 尝试使用数据类型不一致的 REST API 作为响应 - Trying to consume a REST API with inconsistent data types in response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM