小编dam*_*end的帖子

Spring Boot-控制404未找到

我试图找出控制基本Spring Boot RESTful服务的404 Not Found处理程序的最简单方法,例如Spring提供的示例:

https://spring.io/guides/gs/rest-service/

而不是让它返回默认的Json输出:

{
  "timestamp":1432047177086,
  "status":404,
  "error":"Not Found",
  "exception":"org.springframework.web.servlet.NoHandlerFoundException",
  "message":"No handler found for GET /aaa, ..."
}
Run Code Online (Sandbox Code Playgroud)

我想提供自己的Json输出。

通过控制DispatcherServlet和使用DispatcherServlet#setThrowExceptionIfNoHandlerFound(true),我能够使它在404的情况下引发异常,但是我无法通过来处理该异常@ExceptionHandler,就像我处理一样MissingServletRequestParameterException。知道为什么吗?

还是有比抛出并处理NoHandlerFoundException更好的方法?

rest spring json http-status-code-404

5
推荐指数
3
解决办法
1万
查看次数

标签 统计

http-status-code-404 ×1

json ×1

rest ×1

spring ×1