gpt4 book ai didi

java - JBoss + RestEasy + Jackson : org. jboss.resteasy.core.ServerResponse 无法转换为 org.jboss.resteasy.specimpl.BuiltResponse

转载 作者:行者123 更新时间:2023-12-01 12:53:09 29 4
gpt4 key购买 nike

我使用 JBoss (AS 7.1)、RestEasy 和 Jackson 开发 REST Api。 Web 服务返回一个“Account”对象,它是一个简单的 POJO,过去在 JSon 中序列化没有任何问题。

对代码进行一些修改后,服务器在每次调用我的方法后都会弹出异常。执行:

@POST
@Path("/auth")
@Produces(MediaType.APPLICATION_JSON)
public Account authentification(final Account account) throws AuthenticationException {
Logger.debug(this, "Authenticate");
Account returnedAccount = // Some authentication code that build a correct Account object
Logger.debug(this, "Authenticated, return now !");
return returnedAccount;
}

打印输出如下:

(DEBUG) Authenticate
(DEBUG) Authenticated, return now !
(ERROR) java.lang.ClassCastException: org.jboss.resteasy.core.ServerResponse cannot be cast to org.jboss.resteasy.specimpl.BuiltResponse
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:340)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:234)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:221)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
at com.mypackage.filter.MyFilter.doFilter(MyFilter.java:55)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50)
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)
at java.lang.Thread.run(Thread.java:662)

我在谷歌上找到的唯一内容是this ticket关于 JBOSS 问题跟踪器,但没有多大帮助。

任何帮助将不胜感激!

最佳答案

要添加到已接受的答案中,如果 Resteasy 公开的方法返回 null,则也会出现此 ClassCastException。例如:

@POST
@Path("getNotices")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
GetNoticesResponse getNotices(GetNoticesRequest noticesRequest){
return null;
}

通过一些幕后逻辑,自定义 Response 对象 (GetNoticesResponse) 将被 org.jboss.resteasy.core.ServerResponse 替换,然后再次转换为自定义响应,从而触发 ClassCastException。

关于java - JBoss + RestEasy + Jackson : org. jboss.resteasy.core.ServerResponse 无法转换为 org.jboss.resteasy.specimpl.BuiltResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24064841/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com