resteasy 3.1.3.Final and springboot 1.5.7 我想在请求进入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,因此它没有&# 39;工作。它有什么问题?谢谢!
@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&#39; paypal&#39;代码在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
答案 0 :(得分:0)
resteasy文档提供了使用Spring和Spring Boot集成resteasy的简单指南。希望这些链接有用。
如果您按照文档中的说明使用Spring Boot,只需在Application
课程中注册自定义过滤器。
@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。
通过mvn spring-boot:run
运行示例应用。
使用curl测试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
您将在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
希望这有用。