【发布时间】:2017-10-20 03:47:17
【问题描述】:
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。这可以打印我注册的内容,但是在调试请求时注册表/工厂没有我的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;
}
正如 https://github.com/paypal/resteasy-spring-boot 中的“paypal”代码所做的那样,我添加了如下面提到的 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
【问题讨论】:
-
你在
ResouceConfig组件中注册了这个RequestFilter吗? -
@Hantsy ResouceConfig?这个类或接口在哪里以及如何做? Resteasy 3.1.3.Final 没有还是定制的?
-
@Hantsy ResouceConfig 似乎在泽西岛
标签: spring-boot resteasy