除了方法之外,还使用标头将请求路由到带注释的方法 [英] Use header in addition to method to route request to annotated method
问题描述
我想知道JAX-RS是否有可能使用HTTP方法之外的标头来路由请求.实际上,我找不到实现此目的的方法.
I wonder if it's possible with JAX-RS to route a request using an header in addition to the HTTP method. In fact, I can't find out the way to do that.
我想到了类似这样的内容,其中x-header
是标题:
I thought about something like that where x-header
is an header:
@Path("/contacts/")
public class MyResource {
@POST
@Header("x-header:content1")
public void method1(MyContent1 content) {
(...)
}
@POST
@Header("x-header:content2")
public void method2(MyContent2 content) {
(...)
}
}
此问题遵循以下答案:如何更新REST资源收集.
This question follows this answer: How to Update a REST Resource Collection.
非常感谢您的帮助! 蒂埃里
Thanks very much for your help! Thierry
推荐答案
如果您需要影响请求匹配/路由过程,则必须使用JAX-RS过滤器-特定于预匹配的过滤器(@PreMatching)[这将在JAX-RS 2.0及更高版本中可以使用]]如果在资源方法中使用标头信息,则没有意义,因为JAX-RS已经与方法相匹配
If you need to affect the request matching/routing process, you have to use JAX-RS filters - PreMatching filters to be specific (@PreMatching) [this will work in JAX-RS 2.0 onwards] If the use header info in the resource methods, it wont make sense because JAX-RS would have already matched the method
这是过滤器实施的总体流程
Here is the overall flow in filter implementation
- 使用ContainerRequestContext获取标头信息
- 根据标题值应用业务准则
- 现在的诀窍是能够路由到所需的资源方法-您拥有的一个选择是使用ContainerRequestContext的setRequestUri方法,并在不同的URI上设置不同的资源方法(使用@Path)
Jersey文档可能会有所帮助- https://jersey. java.net/documentation/latest/filters-and-interceptors.html#d0e9538
Jersey docs might help -- https://jersey.java.net/documentation/latest/filters-and-interceptors.html#d0e9538
这篇关于除了方法之外,还使用标头将请求路由到带注释的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!