CXF RequestContextFilter

时间:2016-06-09 14:43:43

标签: cxf

http://cxf.apache.org/docs/30-migration-guide.html

  

已删除CXF RequestHandler和ResponseHandler过滤器,   请使用JAX-RS 2.0 ContainerRequestFilter和   ContainerResponseFilter以及WriterInterceptor和   需要时的ReaderInterceptor。

如何重写?我找不到任何将org.apache.cxf.message.Message作为参数并返回Response的示例。

我也应该使用ContainerResponseFilter

public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {

而不是RequestHandler

Response handleRequest(Message m, ClassResourceInfo resourceClass)

1 个答案:

答案 0 :(得分:2)

替换RequestHandler

public class CustomRequestHandler implements RequestHandler {
    public Response handleRequest(Message m, ClassResourceInfo resourceClass) {

ContainerRequestFilter

public class CustomRequestFilter implements ContainerRequestFilter {
    public void filter(ContainerRequestContext context) {
        Message m = JAXRSUtils.getCurrentMessage(); 
        OperationResourceInfo m = message.getExchange().get(OperationResourceInfo.class);
        ClassResourceInfo resourceClass = operation.getClassResourceInfo();
        // finally use context.abortWith(Response) if you need to block the request 

替换ResponseHandler

public class CustomResponseHandler implements ResponseHandler {
    public Response handleResponse(Message m, OperationResourceInfo operation, Response response) {

ContainerResponseFilter

public class CustomResponseFilter implements ContainerResponseFilter  {
    public void filter(ContainerRequestContext inContext, ContainerResponseContext outContext) throws IOException{
        Message m = JAXRSUtils.getCurrentMessage();
        OperationResourceInfo operation = m.getExchange().get(OperationResourceInfo.class);
        //outContext has the capabilites of javax.ws.rs.Response. For example outContext.setStatus(201);

弹簧

<bean id="customRequestFilter" class="com.CustomRequestFilter" />
<bean id="customResponseFilter" class="com.CustomResponseFilter" /> 

<!-- Add filters to provider zone in JAX-RS server-->
<bean id="myRestServer" class="org.apache.cxf.jaxrs.JAXRSServerFactoryBean" lazy-init="false" init-method="create">
    ...
    <property name="providers">
        <list>
            <ref bean="customRequestFilter" />
            <ref bean="customResponseFilter" /> 
        </list>
    </property>