NoSuchMethodError Exception for setEntity in Conta

2019-08-16 23:20发布

I am implementing a JAX-RS filter with CXF and I am trying to edit the Entity of the response. My filter's code is shown below

public void filter(ContainerRequestContext requestContext,
        ContainerResponseContext responseContext) throws IOException {

    String s = (String) responseContext.getEntity();
    responseContext.setEntity(s + "an additional message");
}

The problem is that I am getting an NoSuchMethodError when I am running it.

Here is my Exception stack

javax.ws.rs.InternalServerErrorException: java.lang.NoSuchMethodError: javax.ws.rs.container.ContainerResponseContext.setEntity(Ljava/lang/Object;)V
    at org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.handleWriteException(JAXRSOutInterceptor.java:385)
    at org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.serializeMessage(JAXRSOutInterceptor.java:224)
    at org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.processResponse(JAXRSOutInterceptor.java:154)
    at org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.handleMessage(JAXRSOutInterceptor.java:85)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
    at org.apache.cxf.interceptor.OutgoingChainInterceptor.handleMessage(OutgoingChainInterceptor.java:77)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
    at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
    at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:239)
    at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:223)
    at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:203)
    at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:137)
    at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:158)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:243)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPut(AbstractHTTPServlet.java:180)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:640)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:219)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    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:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NoSuchMethodError: javax.ws.rs.container.ContainerResponseContext.setEntity(Ljava/lang/Object;)V
    at com.my.oasis.filters.TestResponseFilter.filter(TestResponseFilter.java:20)
    at org.apache.cxf.jaxrs.utils.JAXRSUtils.runContainerResponseFilters(JAXRSUtils.java:1675)
    at org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.serializeMessage(JAXRSOutInterceptor.java:219)
    ... 27 more

What am I doing wrong? Is that meaning that the setEntity() method is defined in the API but is not implemented by the CXF Framework?

Thanks

1条回答
Root(大扎)
2楼-- · 2019-08-16 23:32

Try implementing a CXF filter instead of direct JAX-RS filter. For that you will have to implement ResponseHandler interface. Following is dummy code :

package com.question;

import org.apache.cxf.jaxrs.ext.ResponseHandler;
import org.apache.cxf.jaxrs.model.OperationResourceInfo;
import org.apache.cxf.message.Message;


/*
 * Filter for Responses
 */
public class CustomOutfilter implements ResponseHandler
{


    /*
     * @param : arg0
     * 
     * @param : arg1
     * 
     * @param : arg2
     * 
     * @return : Response
     */
    @Override
    public Response handleResponse(Message arg0, OperationResourceInfo arg1,
            Response arg2)
    {
        //play with arg2 here and return it

        return arg2;

    }

}
查看更多
登录 后发表回答