resteasy ContainerRequestFilter 在 springboot 中不起作用

resteasy ContainerRequestFilter didn't work in springboot

resteasy 3.1.3.Final和springboot 1.5.7 我想在请求进入 restful 方法之前做一些事情,但它从来没有奏效。 这是 restful 方法接口。

@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);
}

这是过滤器。

@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("================");
  }
}

它永远不会进入过滤器并打印日志,即使我尝试了注释@Provider/@PreMatching/@Configuration 的任何组合。

后来我想可能是注册表问题,并尝试在@SpringBootApplication中添加@Bean class.This可以打印我注册的内容,但是在调试请求时registry/factory没有我的RequestFilter,因此它没有用。它出什么问题了?谢谢!

@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;
}

正如 'paypal' 代码在 https://github.com/paypal/resteasy-spring-boot 中所做的那样,我像下面提到的 Hantsy 添加了 RequestFilter,它没有用!

这是日志。

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

resteasy 文档提供了将 resteasy 与 Spring 和 Spring Boot 集成的简单指南。希望这些链接对您有所帮助。

如果您按照文档中的描述使用 Spring 启动,只需在 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;
 }  
}

已更新:我分叉了 paypal/resteasy-spring-boot,并修改了示例应用程序,添加了一个 EchoFitler 用于演示目的。

检查source codes from my Github account

  1. 运行 示例应用来自 mvn spring-boot:run.

  2. 使用 curl 测试 api。

    # 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. 您将在 spring-boot 控制台中看到过滤信息。

    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
    

希望这对您有所帮助。