尝试将旧的ERX剩余路由移植到Jerey / JAX-RX时遇到问题。
我正在尝试做这样的事情:
@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(){
...
}
但是,Jersey不想匹配我的http请求。
blahblah.com/v0/user:accessibleWithNoRestriction
我收到没有方法允许的响应。
最佳答案
我遇到了这篇文章:JAX-RS Application on the root context - how can it be done?
尝试使用此:
@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
}
}
然后,您必须将方法中的@Path注释更改为“ / accessibleWithNoRestriction”
这样做是在匹配发生之前更改您的请求的uri。
试试看