Java中的Prematch过滤器重新安装

时间:2014-09-25 22:40:20

标签: java rest servlet-filters resteasy

我有一个像这样的代码块。但它似乎没有找到在@Path中指定了较低的cased url的资源方法(" resource-name")。如何在资源或控制器上的方法匹配之前应用此过滤器?

public class LowerCaseRequestFilter implements Filter {

    @Inject
    public LowerCaseRequestFilter() {
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(@Nonnull ServletRequest servletRequest, @Nonnull ServletResponse servletResponse, @Nonnull FilterChain filterChain) throws IOException, ServletException {

        final HttpServletRequest req = (HttpServletRequest) servletRequest;
        final String requestUrl = req.getRequestURI();
        final String newUrl = requestUrl.toLowerCase();
        req.getRequestDispatcher(newUrl).forward(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }

1 个答案:

答案 0 :(得分:1)

如果您使用的是JAX-RS 2,可以使用@PreMatching ContainerRequestFilter

@Provider
@PreMatching
public class LowerCaseFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        String newUri = requestContext.getUriInfo().getRequestUri().toString().toLowerCase();
        requestContext.setRequestUri(URI.create(newUri));
    }

}