http 405使用containerrequestcontext#setrequesturi路由resteasy请求之后

mqkwyuun  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(311)

我有以下场景:
get请求发送到:http://localhost:8080/api/公司/
containerrequestfilter称为: @Provider @PreMatching public class RequestInterceptor implements ContainerRequestFilter 检查标题: Boolean isTestEnv = Boolean.parseBoolean(requestContext.getHeaderString("isTestEnv")); if(isTestEnv){ 如果为true,则uri将更改: String url = requestContext.getUriInfo().getAbsolutePath().toString(); String newURL = url.replaceFirst("/API/company", "/APISimulator/company/test"); 请求被转发: requestContext.setRequestUri(new URI(newURL)); 当我请求:http://localhost:8080/api/company/触发过滤器并执行代码,但发生http 405。

HTTP/1.1 405 Method Not Allowed
Allow: OPTIONS
Connection: keep-alive
X-Powered-By: Undertow/1
Server: WildFly/9
Content-Length: 0
Date: Tue, 31 Oct 2017 19:29:21 GMT

如果我删除标头,请求将成功发生

HTTP/1.1 200 OK
Connection: keep-alive
X-Powered-By: Undertow/1
Server: WildFly/9
Content-Type: application/json
Content-Length: 2
Date: Tue, 31 Oct 2017 19:43:31 GMT

OK

日志:

2017-10-31 17:29:21,836 WARN  [org.jboss.resteasy.core.ExceptionHandler] (default task-48) failed to execute: javax.ws.rs.NotAllowedException: No resource method found for GET, return 405 with Allow header
    at org.jboss.resteasy.core.registry.SegmentNode.match(SegmentNode.java:375)
    at org.jboss.resteasy.core.registry.SegmentNode.match(SegmentNode.java:114)
    at org.jboss.resteasy.core.registry.RootNode.match(RootNode.java:43)
    at org.jboss.resteasy.core.registry.RootClassNode.match(RootClassNode.java:48)
    at org.jboss.resteasy.core.ResourceMethodRegistry.getResourceInvoker(ResourceMethodRegistry.java:444)
    at org.jboss.resteasy.core.SynchronousDispatcher.getInvoker(SynchronousDispatcher.java:234)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:171)
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86)
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:72)
    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
    at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:282)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:261)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:80)
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:172)
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:199)
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:774)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

我做错什么了?
版本:
wildfly 9.0.2版
resteasy 3.0.11版

rryofs0p

rryofs0p1#

我将拦截器更改为使用以下webfilter:

@WebFilter("/*")
public class RequestInterceptor implements Filter {

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

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        second(request, response, chain);
    }

    private void second(ServletRequest request, ServletResponse response, FilterChain chain) {
        HttpServletResponse res = (HttpServletResponse) response;
        HttpServletRequest httpRequest = (HttpServletRequest) request;

        String isTestEnv = httpRequest.getHeader("isTestEnv");

        if(isTestEnv != null && isTestEnv.equals("true")) {

            String url = getFullURL(httpRequest);
            String newURL = url.replaceFirst("/API/company", "/APISimulator/company/test");

            res.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
            res.setHeader("Location", newURL);  

        } else {
            try {
                chain.doFilter(request, response);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ServletException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void destroy() {

    }

    public static String getFullURL(HttpServletRequest request) {
        StringBuffer requestURL = request.getRequestURL();
        String queryString = request.getQueryString();

        if (queryString == null) {
            return requestURL.toString();
        } else {
            return requestURL.append('?').append(queryString).toString();
        }
    }

}

请求被成功转发。

相关问题