gpt4 book ai didi

java - 请求后发生 "javax.ws.rs.NotFoundException: HTTP 404 Not Found"

转载 作者:行者123 更新时间:2023-12-05 06:15:37 27 4
gpt4 key购买 nike

我正在开发一个 Java8 Tomcat8 Jersey2 网络应用程序,它在成功响应请求后随机记录一个奇怪的异常。我为所有请求设置了一个过滤器,以执行一些日志记录并向 MDC 添加一些值(例如 requestId),异常来自此处。这是堆栈:

- HTTP 404 Not Found 
javax.ws.rs.NotFoundException: HTTP 404 Not Found
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:323)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.xyz.gatorws.webapp.LoggingFilter.doFilter(LoggingFilter.java:34)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:609)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:810)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1623)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

奇怪的是请求被成功处理并且成功响应(200 在这个堆栈出现之前发送回调用客户端。我很茫然。另外,堆栈中的行来 self 的代码 (com.xyz.gatorws.webapp.LoggingFilter.doFilter(LoggingFilter.java:34)) 如下:

chain.doFilter(httpRequest, response);

考虑到过滤器在处理请求之前运行,这很奇怪。啊!我肯定错过了什么。我清除了 LoggingFilter.destroy() 方法中的日志记录上下文,因此我看不到涉及另一个请求,但我真的不知所措。

如有任何想法,我们将不胜感激。

最佳答案

如果这可以帮助提问者或其他人

我的配置是 Java9 Tomcat9 Jersey2。我遇到了这样一个问题,因为匹配的方法没有用任何 HTTP 方法注释进行注释。在我的例子中,@GET 丢失了。该方法返回一个 Response 对象,在 return Response ... 之前的最后一个语句之后它抛出 NotFoundException。我认为 Jersey 认为此方法是由于 @Path 注释而找到的子资源定位器。

所以我改变了这个:

@Path("unique/page")
@Consumes("*/*")
@RolesAllowed("owner")
public Response getUnique(@PathParam("userUUID") String userUUID) throws RuntimeException {
LogNdc.log("userUUID: " + userUUID);
SearchCompanieService scs = new SearchCompanieService();
CompanyPagePojo cpp = scs.searchByFieldNameAutoCommit("userPojoOwnerId", userUUID);
System.out.println(cpp);
return Response.status(Status.OK).entity(cpp).build();
}

对此:

@Path("unique/page")
@GET
@Consumes("*/*")
@RolesAllowed("owner")
public Response getUnique(@PathParam("userUUID") String userUUID) throws RuntimeException {
LogNdc.log("userUUID: " + userUUID);
SearchCompanieService scs = new SearchCompanieService();
CompanyPagePojo cpp = scs.searchByFieldNameAutoCommit("userPojoOwnerId", userUUID);
System.out.println(cpp);
return Response.status(Status.OK).entity(cpp).build();

但在我的例子中,由于异常,请求没有生成任何 200 响应代码,客户端得到 500,因为我有一个 RuntimeExceptionMapper。

关于java - 请求后发生 "javax.ws.rs.NotFoundException: HTTP 404 Not Found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62413076/

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