Search code examples
spring-bootresteasy

resteasy ContainerRequestFilter didn't work in springboot


resteasy 3.1.3.Final and springboot 1.5.7 I want do somthing before the request go ino the restful method,but it never worked. here is the restful method interface.

@Path("/demo")
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
public interface DemoService {

  @POST
  @Path("/query")
  List<EntityDemoInfo> queryByType(QueryRequest requst);
}

Here is the filter.

@Provider
@PreMatching
public class RequestFilter implements HttpRequestPreprocessor,ContainerRequestFilter{
  @Override
  public void filter(ContainerRequestContext requestContext) throws IOException {
    System.out.println("-----------------");
  }

  @Override
  public void preProcess(HttpRequest request) {
    System.out.println("================");
  }
}

It never go in the filter and print the log,even if i tried the annotations @Provider/@PreMatching/@Configuration in any combination.

Later i think maybe something registry problem,and tried to add @Bean in @SpringBootApplication class.This can print what I register,however when debugging request the registry/factory din't have my RequestFilter, thus it didn't work. What's wrong with it? thanks !

@Bean
public SynchronousDispatcher synchronousDispatcher() {
    ResteasyProviderFactory providerFactory = ResteasyProviderFactory.getInstance();
    RequestFilter requestFilter = new RequestFilter();
    providerFactory.getContainerRequestFilterRegistry().registerSingleton(requestFilter);
    SynchronousDispatcher dispatcher = new SynchronousDispatcher(providerFactory);
    dispatcher.addHttpPreprocessor(requestFilter);
    System.out.println("*****************");
    System.out.println(providerFactory.getContainerRequestFilterRegistry().preMatch());
    return dispatcher;
}

As 'paypal' codes do in https://github.com/paypal/resteasy-spring-boot , I added RequestFilter like Hantsy mentioned below, it didn't work!

Here is the log.

14:44:01.537 [main] INFO org.apache.tomcat.util.net.NioSelectorPool Using a shared selector for servlet write/read
14:44:01.548 [main] INFO org.jboss.resteasy.resteasy_jaxrs.i18n RESTEASY002225: Deploying javax.ws.rs.core.Application: class com.sample.app.JaxrsApplication
@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@   ------This is what I add in JaxrsApplication
14:44:01.548 [main] INFO org.jboss.resteasy.resteasy_jaxrs.i18n RESTEASY002215: Adding singleton provider java.lang.Class from Application class com.sample.app.JaxrsApplication
14:44:01.554 [main] INFO org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer Tomcat started on port(s): 8080 (http)
14:44:01.559 [main] INFO com.sample.app.Application Started Application in 2.478 seconds (JVM running for 2.978)



//There is when i post a request as it say what happened,nothing,but got the response.Thus it didn't work!



14:45:58.657 [RMI TCP Connection(2)-127.0.0.1] INFO org.springframework.boot.admin.SpringApplicationAdminMXBeanRegistrar$SpringApplicationAdmin Application shutdown requested.
14:45:58.657 [RMI TCP Connection(2)-127.0.0.1] INFO org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@34f22f9d: startup date [Fri Oct 20 14:43:59 CST 2017]; root of context hierarchy
14:45:58.659 [RMI TCP Connection(2)-127.0.0.1] INFO org.springframework.context.support.DefaultLifecycleProcessor Stopping beans in phase 0
14:45:58.660 [RMI TCP Connection(2)-127.0.0.1] INFO org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter Unregistering JMX-exposed beans on shutdown
14:45:58.660 [RMI TCP Connection(2)-127.0.0.1] INFO org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter Unregistering JMX-exposed beans
14:45:58.660 [RMI TCP Connection(2)-127.0.0.1] INFO org.springframework.jmx.export.annotation.AnnotationMBeanExporter Unregistering JMX-exposed beans on shutdown

Solution

  • The resteasy documentation provides simple guide for intgrating resteasy with Spring and Spring Boot. Hope these links are helpful.

    If you are using Spring Boot as described in the doc, just register you custom Filter in your Application class.

    @Component
    @ApplicationPath("/sample-app/")
    public class JaxrsApplication extends Application {
    
    @Override
    public Set<Object> getSingletons() {
    
        Set<Object> singletons = new HashSet<>();
        singletons.add(yourFilter);
        return singletons;
     }  
    }
    

    Updated: I forked the paypal/resteasy-spring-boot, and modified the sample-app, added a EchoFitler for demo purpose.

    Check the source codes from my Github account.

    1. Run the sample-app via mvn spring-boot:run.

    2. Use curl to test the apis.

      # curl -v -X POST -H "Content-Type:text/plain" -H "Accept:application/json" http://localhost:8080/sample-app/echo -d "test"
      Note: Unnecessary use of -X or --request, POST is already inferred.
      *   Trying ::1...
      * TCP_NODELAY set
      * Connected to localhost (::1) port 8080 (#0)
      > POST /sample-app/echo HTTP/1.1
      > Host: localhost:8080
      > User-Agent: curl/7.56.0
      > Content-Type:text/plain
      > Accept:application/json
      > Content-Length: 4
      >
      * upload completely sent off: 4 out of 4 bytes
      < HTTP/1.1 200
      < X-Application-Context: application
      < Content-Type: application/json
      < Content-Length: 45
      < Date: Fri, 20 Oct 2017 07:19:43 GMT
      <
      {"timestamp":1508483983603,"echoText":"test"}* Connection #0 to host localhost left intact
      
    3. And you will see the filtering info in the spring-boot console.

      filtering request context:org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext@1ca8d1e4
      filtering request/response context:org.jboss.resteasy.core.interception.jaxrs.ResponseContainerRequestContext@1787a18c
       org.jboss.resteasy.core.interception.jaxrs.ContainerResponseContextImpl@4aad828e
      

    Hope this is helpful.