[英]Concatenate class @Path annotation to method @Path annotation when using colon
I ran into a problem when trying to port our old ERX rest routes to Jerey/JAX-RX. 我试图将旧的ERX休息路线移植到Jerey / JAX-RX时遇到了问题。
I am trying to do something like this: 我想做这样的事情:
@Path("/v0/user")
@Controller
public class UserRouteController {
@GET
public Response getAllUsers(){
...
}
@GET
@Path("/{name}")
public Response getUserWithName(@PathParam("name") String name){
...
}
@GET
@Path(":accessibleWithNoRestriction")
public Response getUsersAccessibleWithNoRestriction(){
...
}
@GET
@Path(":withAdminStatus")
public Response getUsersWithAdminStatus(){
...
}
However, Jersey does not want to match my http request. 但是,泽西岛不想匹配我的http请求。
blahblah.com/v0/user:accessibleWithNoRestriction
I get a No Method Allowed response. 我得到一个No Method Allowed响应。
I normally don't include the leading/trailing /
's in my paths, so I'm pretty sure @Path("rootLevel")
and @Path("methodLevel")
actually maps to rootLevel/methodLevel
not rootLevelMethodLevel
. 我通常不会在路径中包含前导/尾随
/
,因此我非常确定@Path("rootLevel")
和@Path("methodLevel")
实际上映射到rootLevel/methodLevel
而不是 rootLevelMethodLevel
。
So in your example, @Path("/v0/user")
and @Path(":withAdminStatus")
maps to /v0/user/:withAdminStatus
. 因此,在您的示例中,@
@Path("/v0/user")
和@Path(":withAdminStatus")
映射到/v0/user/:withAdminStatus
。 Try changing your paths to something like this: 尝试将路径更改为以下内容:
@Path("v0")
@Controller
public class UserRouteController {
@GET
@Path("user")
public Response getAllUsers(){
//...
}
@GET
@Path("user/{name}")
public Response getUserWithName(@PathParam("name") String name){
//...
}
@GET
@Path("user:accessibleWithNoRestriction")
public Response getUsersAccessibleWithNoRestriction(){
//...
}
@GET
@Path("user:withAdminStatus")
public Response getUsersWithAdminStatus(){
//...
}
}
Alternatively, you might be able to pull something off with some kind of redirection. 或者,您可以通过某种重定向来解决问题。 For example, with a Pre-matching Filter .
例如,使用预匹配过滤器 。 I've never done anything like this, but the documentation suggests that "you can even modify request URI".
我从来没有做过这样的事情,但是文档建议“你甚至可以修改请求URI”。 With that in mind, you could replace any request with
:withAdminStatus
in the URI with /:withAdminStatus
so that it can be matched with the correct resource. 考虑到这一点,您可以使用带有
/:withAdminStatus
的URI中的:withAdminStatus
替换任何请求,以便它可以与正确的资源匹配。
I came across this post: JAX-RS Application on the root context - how can it be done? 我遇到过这篇文章: 根文本上的JAX-RS应用程序 - 如何完成?
Try using this: 试试这个:
@WebFilter(urlPatterns = "/*")
public class PathingFilter implements Filter {
Pattern[] restPatterns = new Pattern[] {
Pattern.compile("/v0/user:.*")
};
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (request instanceof HttpServletRequest) {
String path = ((HttpServletRequest) request).getPathInfo();
for (Pattern pattern : restPatterns) {
if (pattern.matcher(path).matches()) {
String[] segments = path.split(":");
String newPath = segments[0] + "/" + segments[1];
newPath = ((HttpServletRequest) request).getServletPath() + "/" + newPath;
request.getRequestDispatcher(newPath).forward(request, response);
return;
}
}
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
}
Then you'll have to change the @Path annotation in your method to "/accessibleWithNoRestriction" 然后你必须将方法中的@Path注释更改为“/ accessibleWithNoRestriction”
What this would do is change the uri of your request before the matching happens. 这样做是在匹配发生之前更改请求的uri。
Try that 试试吧
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.