简体   繁体   中英

JBoss + RestEasy + Jackson : org.jboss.resteasy.core.ServerResponse cannot be cast to org.jboss.resteasy.specimpl.BuiltResponse

I work on a REST Api with JBoss (AS 7.1), RestEasy and Jackson. The web service return an "Account" object, it's a simple POJO that used to be serialized in JSon without any problem.

After some modifications in the code, the server pop an exception after each call to my method. The execution of :

@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;
}

Print an output as following :

(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)

The only thing i found on google was this ticket on JBOSS issues tracker but it doesn't help much.

Any help would be much appreciated !

To add to the accepted answer, this ClassCastException can also appear if your methods exposed by resteasy return null. For example:

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

By some under-the-scenes logic, the custom Response object (GetNoticesResponse) will get replaced by org.jboss.resteasy.core.ServerResponse and then cast into the custom response again, triggering the ClassCastException.

After lot of investigation, the bug came from a conflict in JBoss libraries. It cames out after adding a JMS Queue to our JBOSS configuration.

To resolve it we first kicked out all our dependencies to javax.ws.rs-api-xx < 2.0, then we updated to javax.ws.rs-api-2.0 on our servers.

To do that :

  • Add the new Jar
wget https://repository.liferay.com/nexus/content/groups/public/javax/ws/rs/javax.ws.rs-api/2.0/javax.ws.rs-api-2.0.jar -O /usr/local/bin/jboss-as-7.1.1.Final/modules/javax/ws/rs/api/main/javax.ws.rs-api-2.0.jar && md5sum /usr/local/bin/jboss-as-7.1.1.Final/modules/javax/ws/rs/api/main/javax.ws.rs-api-2.0.jar

Be sure the printed MD5 is 3bc8176d36becb7746e1f2594346ed66, otherwise it's not the good package.

  • Update module.xml

Again in "/usr/local/bin/jboss-as-7.1.1.Final/modules/javax/ws/rs/api/main" change the module.xml file to match this content :

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="javax.ws.rs.api">
<resources>
    <resource-root path="javax.ws.rs-api-2.0.jar"/>
</resources>
<dependencies>
    <module name="org.jboss.resteasy.resteasy-jaxrs" services="export"/>
</dependencies>
</module>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM