gpt4 book ai didi

java - 避免 "No mapping found for HTTP request with URI"或 catch-all Controller

转载 作者:行者123 更新时间:2023-11-30 01:56:44 28 4
gpt4 key购买 nike

避免以下警告的最佳方法是什么?

o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [......]

我尝试添加一个包罗万象的 Controller ,如下所示:

@Controller
class CatchAllController {
@RequestMapping(value = "/**")
public ResponseEntity<?> catchAll() {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}

但由于某种原因,静态内容(即src/main/resources/static/)也被 Controller 捕获,因此无法访问。

另一方面,我不希望这些警告出现在日志中,因为客户端可能会访问随机 URI,而我不希望收到相关通知。

最佳答案

On the other hand, I don't want these warnings to appear in the logs since clients may access random URI and I don't want to be notified about it.

然后只需禁用记录这些警告的记录器即可。

截至Spring 5.1.4.RELEASE ,这些是通过这些成员从 DispatcherServlet 记录的

/** Log category to use when no mapped handler is found for a request. */
public static final String PAGE_NOT_FOUND_LOG_CATEGORY = "org.springframework.web.servlet.PageNotFound";

/** Additional logger to use when no mapped handler is found for a request. */
protected static final Log pageNotFoundLogger = LogFactory.getLog(PAGE_NOT_FOUND_LOG_CATEGORY);

使用 Logback,您可以声明类似的内容

<logger name="org.springframework.web.servlet.PageNotFound" level="ERROR">
...
</logger>

同样,使用 log4j2,您也可以这样做

<Logger name="org.springframework.web.servlet.PageNotFound" level="ERROR">
...
</Logger>

关于java - 避免 "No mapping found for HTTP request with URI"或 catch-all Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54203239/

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