如您所知,在XML中,配置它的方法是:
<error-page>
<error-code>404</error-code>
<location>/my-custom-page-not-found.html</location>
</error-page>
Run Code Online (Sandbox Code Playgroud)
但我还没有找到一种方法在Java配置中执行此操作.我尝试的第一种方式是:
@RequestMapping(value = "/**")
public String Error(){
return "error";
}
Run Code Online (Sandbox Code Playgroud)
它似乎工作,但它检索资源有冲突.
有办法吗?
我试图找出控制基本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更好的方法?
我想在浏览器中显示控制器返回的 ResponseEntity 的主体(使用 Spring):
return new ResponseEntity<>(l.getReachableDate(), HttpStatus.NOT_FOUND);
l.getReachableDate()返回一个 Date 类型,我想以如下方式显示它:
<header>
<h1><span>Url is not reachable from</span> <!-- body --> </h1>
</header>
Run Code Online (Sandbox Code Playgroud)
我怎样才能让它显示出来?