NoSuchMethodError ContainerResponseFilter中setEntity的异常

时间:2014-04-07 11:05:19

标签: java rest cxf jax-rs nosuchmethoderror

我正在使用 CXF 实施 JAX-RS filter,我正在尝试编辑响应的实体。我的过滤器代码如下所示

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

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

问题是我在运行时遇到NoSuchMethodError

这是我的异常堆栈

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

我做错了什么?这是否意味着setEntity()方法在API中定义但未由 CXF框架实现?

由于

1 个答案:

答案 0 :(得分:0)

尝试实施CXF filter而不是直接JAX-RS filter。为此,您必须实现ResponseHandler接口。 以下是虚拟代码:

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;

    }

}