从 CXF 2.7 升级到 CXF 3.0

Upgrading from CXF 2.7 to CXF 3.0

我正在尝试将其中一个代码库升级到 CXF 3.0,并且一些 类 已被弃用并尝试升级依赖项。

@Override
public Response handleResponse(Message m, OperationResourceInfo ori,
        Response response) {
    if (response.getStatus() == Response.Status.OK.getStatusCode()){
        if (response.getHeaderString("my_header") == null){
            Message inMessage = m.getExchange().getInMessage();
            String headerKey = getMyHeaderKey(inMessage);
            if (headerKey != null){
                AbstractResponse entityResponse  = (AbstractResponse) response.getEntity();
                response = generateResponse(entityResponse, inMessage, false);
            }
        }
    }
    return response;
}

private Response generateResponse(AbstractResponse ar, Message msg, boolean isConflict){
    ResponseBuilder responseBldr;
    if (isConflict){
        responseBldr = Response.status(Status.CONFLICT);
    }
    else{
        responseBldr = Response.ok(ar);
    }
    responseBldr.header("header1", "true");
    HttpServletRequest request = (HttpServletRequest) msg.get(AbstractHTTPDestination.HTTP_REQUEST);
    String retryId = request.getHeader("header2");
    if (retryId != null){
        responseBldr.header("header2", retryId);
    }
    return responseBldr.build();
}

我尝试使用 ContainerRequest/Response 过滤器,但找不到如何设置响应

@Override
public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {
    Message m = JAXRSUtils.getCurrentMessage();
    containerResponseContext.getStatus();

    if (containerResponseContext.getStatus() == Response.Status.OK.getStatusCode()){
        if (containerResponseContext.getHeaderString("my_header") == null){
            Message inMessage = m.getExchange().getInMessage();
            String headerKey = getMyHeaderKey(inMessage);
            if (headerKey != null){
                AbstractResponse entityResponse  = (AbstractResponse) containerResponseContext.getEntity();
                response = generateResponse(entityResponse, inMessage, false);   //how do I do this with CXF 3.0?
            }
        }
    }

}

请参阅 JAX-RS 2.0 过滤器 here,其中介绍了新的过滤器 ContainerRequestFilter 和 ContainerResponseFilter。下面是一个示例,简而言之,它完成了您试图实现的所有操作。

public class KPFilter implements ContainerResponseFilter {

    private Logger LOG = LoggerFactory.getLogger(KPFilter.class);

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

        if(responseContext.getStatus()== Status.OK.getStatusCode()){
            final String myHeader = requestContext.getHeaderString("myHeader");
            if(myHeader !=null && myHeader.equals("kp-header")){
                responseContext.getHeaders().add("resHeader", myHeader+"-res");
            }else{
                responseContext.setEntity("An erro occured");
                responseContext.setStatus(500);
            }
        }else{
            LOG.info("Status is not OK, its {}", responseContext.getStatus());
        }

    }
}

和cxf配置文件

<jaxrs:providers>
            <bean class="com.xxxxx.xxxxx.KPFilter" />
</jaxrs:providers>