ContainerRequestFilter ContainerResponseFilter doe

2020-02-04 11:21发布

I am trying to learn jersey by creating a small RESTful service. I want to use the Filters for specific reasons(Like I want to use the ContainerResponseFilter for CORS headers to allow cross domain requests). However, I am just not able to get these filters intercept my call. I have seen all the posts for this problem and most of them say to register with annotation provider or in web.xml. I have tried registering the files in web.xml as well as giving a @Provider annotation for the container

Here is my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container, 
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html#d4e194 -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/spring/config/BeanLocations.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.rest.example</param-value>
        </init-param>
        <init-param>  
            <param-name>jersey.config.server.provider.packages</param-name>  
            <param-value>com.rest.example.cors</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
            <param-value>com.rest.example.CORSFilter</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
            <param-value>com.rest.example.RequestFilter</param-value>
        </init-param>

    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webresources/*</url-pattern>
    </servlet-mapping>

</web-app>

Here are my Filters:

package com.rest.example.cors;

import javax.ws.rs.ext.Provider;

import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;

@Provider
public class CORSFilter implements ContainerResponseFilter {

    public ContainerResponse filter(ContainerRequest creq,
            ContainerResponse cresp) {

             cresp.getHttpHeaders().putSingle("Access-Control-Allow-Origin", "*");
             cresp.getHttpHeaders().putSingle("Access-Control-Allow-Credentials", "true");
             cresp.getHttpHeaders().putSingle("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS, HEAD");
             cresp.getHttpHeaders().putSingle("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");

            return cresp;
    }
}


package com.rest.example.cors;

import javax.ws.rs.ext.Provider;

import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerRequestFilter;

@Provider
public class RequestFilter implements ContainerRequestFilter {

    public ContainerRequest filter(ContainerRequest request) {
        System.out.println("request filter");
        return request;
    }
}

Link to my github project.

4条回答
疯言疯语
2楼-- · 2020-02-04 11:47

I solved the problem on Wildfly 10 / rest easy like this (CORSFilter is my ContainerResponseFilter):

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/rest")
public class JaxRsActivator extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        final Set<Class<?>> resources = new HashSet<Class<?>>();

        resources.add(CORSFilter.class);

        return resources;
    }
}
查看更多
Melony?
3楼-- · 2020-02-04 11:49

If you are extending the ResourceConfig class the process of registering all the providers can be tedious one and chances are there that one can even miss few providers.

What here can be done with a type of ResourceConfig is you can use packages method in the default constructor to specify the packages("") which will contain your rest resources and the providers. For Instance lets say we have a package com.test.poc.rest which contains all the rest services and another package namely com.test.poc.providers then our resourceConig will look like:

public class CustomResourceConfig extends ResourceConfig{
    public CustomResourceConfig(){
       super();
       packages("com.test.poc.rest;com.test.poc.providers");
       //register any custom features
       register(JacksonFeature.class);         // enabling JSON feature.
    }
}

and boom jersey will now scan for your webservices annotated with @Path and for the providers annotated with @Provider.

查看更多
爷、活的狠高调
4楼-- · 2020-02-04 12:00

<init-param>
  <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
  <param-value>org.anchepedheplatform.infrastructure.core.filters.ResponseCorsFilter</param-value>
</init-param>

First, I wrote a class which implements com.sun.jersey.spi.container.ContainerResponseFilter

import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;

/**
* Filter that returns a response with headers that allows for Cross-Origin
* Requests (CORs) to be performed against the platform API.
*/
public class ResponseCorsFilter implements ContainerResponseFilter {

  @Override
  public ContainerResponse filter(final ContainerRequest request, final ContainerResponse   response) { 
      final ResponseBuilder resp = Response.fromResponse(response.getResponse());
      resp.header("Access-Control-Allow-Origin", "*")
      .header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
      final String reqHead = request.getHeaderValue("Access-Control-Request-Headers");
      if (null != reqHead && !reqHead.equals(null)) {
      resp.header("Access-Control-Allow-Headers", reqHead);}
      response.setResponse(resp.build());
      return response;
 }

and later I had put this reference of this class in intit-param web.xml.

查看更多
何必那么认真
5楼-- · 2020-02-04 12:07

I added a Jersey Application class and registered the filter in the class, which solved my problem. Also upgraded my jersey version to 2.x from 1.x

public class MyApplication extends ResourceConfig {

    /**
     * Register JAX-RS application components.
     */
    public MyApplication () {
        register(RequestContextFilter.class);
        register(JacksonFeature.class);
        register(CustomerResource.class);
        register(Initializer.class);
        register(JerseyResource.class);
        register(SpringSingletonResource.class);
        register(SpringRequestResource.class);
        register(CustomExceptionMapper.class);
    }
}
查看更多
登录 后发表回答