gpt4 book ai didi

java - 找不到选项的资源方法,返回 OK,并带有允许 header : org. jboss.resteasy.spi.DefaultOptionsMethodException

转载 作者:行者123 更新时间:2023-12-02 02:40:56 26 4
gpt4 key购买 nike

我试图从我的 Ionic 3 应用程序向 Java 资源发出发布请求,但它引发了标题中的错误,下面是我发出请求的方法:

@POST
@Path("/loginCpf")
@Produces(MediaType.APPLICATION_JSON)
public Integer validarBeneficiarioCpf(BeneficiarioLoginTO beneficiario) throws Exception, TypeWarnWebException {
HttpSession session = (HttpSession) request.getSession(true);
Integer numOpcoes = beneficiarioLoginNegocio.validarCpfLogin(beneficiario);
if(session.getAttribute("beneficiario") == null && numOpcoes == null){
return -1;
}
return numOpcoes;
}

以及抛出的错误:

09:32:0 3,649 ERROR [br.gov.transportes.spl.web.exception.provider.ExceptionResourceMapper] (http--0.0.0.0-443-3) No resource method found for options, return OK with Allow header: org.jboss.resteasy.spi.DefaultOptionsMethodException: No resource method found for options, return OK with Allow header
at org.jboss.resteasy.core.registry.Segment.match(Segment.java:107) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.registry.SimpleSegment.matchSimple(SimpleSegment.java:33) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.registry.RootSegment.matchChildren(RootSegment.java:327) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.registry.SimpleSegment.matchSimple(SimpleSegment.java:44) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.registry.RootSegment.matchChildren(RootSegment.java:327) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.registry.RootSegment.matchRoot(RootSegment.java:374) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.registry.RootSegment.matchRoot(RootSegment.java:367) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.ResourceMethodRegistry.getResourceInvoker(ResourceMethodRegistry.java:307) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.getInvoker(SynchronousDispatcher.java:173) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:118) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.2.Final.jar:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at br.gov.transportes.spl.web.filter.CacheFilter.doFilter(CacheFilter.java:36) [classes:]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_79]

这是我的 Ionic 2 请求方法:

  getUser() {
return new Promise((resolve, reject) => {
const body = {
'nrCpf': '00000000000',
'dtNascimento': ''
};

let headers = new Headers({
'Content-Type': 'application/json'
});

let options = new RequestOptions({
headers: headers
});

this.http.post(this.API_URL, body, options)
.subscribe((data: any) => {
resolve(data);
}, (err: any) => {
reject(err);
});
});
}

当我调试应用程序时,请求落入此过滤器:

@WebFilter("*")
public class CacheFilter implements Filter {


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

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
HttpServletResponse httpResponse = (HttpServletResponse) response;
HttpServletRequest httpRequest = (HttpServletRequest) request;
String page = httpRequest.getRequestURI();


httpResponse.setHeader("Access-Control-Allow-Origin","*");
httpResponse.setHeader("Access-Control-Allow-Methods","GET,POST");
httpResponse.setHeader("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept");

if (!page.contains("fontawesome-webfont.eot") && !page.contains("/js/lib/") && !page.contains("/css/")) {
httpResponse.setHeader("Expires", "-1");
httpResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
httpResponse.setHeader("Pragma", "no-cache");
} else {
httpResponse.setHeader("Cache-Control", "max-age=" + 604800);
httpResponse.setDateHeader("Last-Modified", new Date().getTime());
}
chain.doFilter(request, response);
}

@Override
public void destroy() {
}
}

当我使用 postman 发出相同的请求时,chain.doFilter(request, response);会抛出错误,该函数可以工作,但我使用原始数据和内容发出请求 -输入 application/json,在我的 Ionic 应用程序中,我制作了 header content-tyoe application/json 并引发了相同的错误。

最佳答案

实际问题的发生是因为 JavaScript 运行时在 CORS 的情况下使用 HTTP OPTIONS 方法执行所谓的“预检”请求。更多详情可查看here 。如果您未在服务器中提供 OPTIONS 方法,则会导致所描述的异常。因此,您有两种选择来解决此问题:

  1. 您在 REST 服务中创建一个用“@OPTIONS”注释的方法,如下所示:

    @OPTIONS
    @Path("/loginCpf")
    public Response optionsBeneficiarioCpf() {
    return Response.ok().header(HttpHeaders.ALLOW, HttpMethod.POST).build();
    }
  2. 您以通用方式处理 OPTIONS 请求,例如在 CORS 过滤器中,如 here 中所述。 .

关于java - 找不到选项的资源方法,返回 OK,并带有允许 header : org. jboss.resteasy.spi.DefaultOptionsMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45484621/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com