Java 尝试将类对象列表作为响应发送到REST API
我试图发送NotificationEntity类对象的列表作为对RESTAPI调用的响应。 当调用时,查询将通过数据库运行,该数据库将返回NotificationEntity类对象的列表,当我尝试将列表作为JSON发送时,我得到的错误是 Ljava.lang.Object;不能投 下面给出了完整的堆栈跟踪以及我的代码 我的控制器是:Java 尝试将类对象列表作为响应发送到REST API,java,json,spring,rest,Java,Json,Spring,Rest,我试图发送NotificationEntity类对象的列表作为对RESTAPI调用的响应。 当调用时,查询将通过数据库运行,该数据库将返回NotificationEntity类对象的列表,当我尝试将列表作为JSON发送时,我得到的错误是 Ljava.lang.Object;不能投 下面给出了完整的堆栈跟踪以及我的代码 我的控制器是: @RequestMapping(value = "/reconciler/{user}", method = RequestMethod.GET, produces
@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;
}
POJO包装器:
public class NotificationEntityWrapper {
private List<NotificationEntity> notificationEntityList;
public List<NotificationEntity> getNotificationEntityList() {
return notificationEntityList;
}
public void setNotificationEntityList(List<NotificationEntity> notificationEntityList) {
this.notificationEntityList = notificationEntityList;
}
}
您是否尝试使用@ResponseBody of Spring?,例如:
@RequestMapping(value = "/reconciler/{user}", method = RequestMethod.GET)
@ResponseBody
List<NotificationEntity> getUnProcessedNotifications(@PathVariable(value = "user") String user) {
...
@RequestMapping(value=“/reconciler/{user}”,method=RequestMethod.GET)
@应答器
列出getUnProcessedNotifications(@PathVariable(value=“user”)字符串用户){
...
尝试将控制器中的@PathVariable
更改为@RequestBody
,如下所示
@RequestMapping(value = "/reconciler/{user}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity getUnProcessedNotifications(@RequestBody NotificationEntity user) {
}
我看到您正在使用spring,您是否只是尝试使用注释@ResponseBody并返回对象列表?这应该序列化到JSON您是否可以尝试从类NotificationEntity中实现Serializable?您是否可以尝试从toString方法中删除日期?如果仍然失败,我将尝试使用相同的数据制作一个真实的示例,然后重试产生同样的问题,但我需要进一步处理的日期。嗨,你能切换你的界面,使其扩展JpaRepository吗?(而不是扩展JpaRepository)
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)
@RequestMapping(value = "/reconciler/{user}", method = RequestMethod.GET)
@ResponseBody
List<NotificationEntity> getUnProcessedNotifications(@PathVariable(value = "user") String user) {
...
@RequestMapping(value = "/reconciler/{user}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity getUnProcessedNotifications(@RequestBody NotificationEntity user) {
}