NoSuchMethodError Exception for setEntity in ContainerResponseFilter

 ̄綄美尐妖づ 提交于 2019-12-11 18:06:59

问题


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:


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;

    }

}


来源:https://stackoverflow.com/questions/22910580/nosuchmethoderror-exception-for-setentity-in-containerresponsefilter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!