问题描述
当找不到请求的资源时,我正在寻找一种干净的方法来返回 Spring 4 中的自定义 404 错误页面.对不同域类型的查询应该会导致不同的错误页面.
I'm looking for a clean way to return customized 404 errorpages in Spring 4 when a requested resource was not found. Queries to different domain types should result in different error pages.
这里有一些代码来表明我的意图(Meter 是一个域类):
Here some code to show my intention (Meter is a domain class):
@RequestMapping(value = "/{number}", method = RequestMethod.GET)
public String getMeterDetails(@PathVariable("number") final Long number, final Model model) {
final Meter result = meterService.findOne(number);
if (result == null) {
// here some code to return an errorpage
}
model.addAttribute("meter", result);
return "meters/details";
}
我想了几种处理问题的方法.首先,有可能创建 RuntimeException
像
I imagine several ways for handling the problem. First there would be the possibility to create RuntimeException
s like
@ResponseStatus(HttpStatus.NOT_FOUND)
public class MeterNotFoundExcption extends RuntimeException { }
然后使用异常处理程序呈现自定义错误页面(可能包含指向仪表列表的链接或任何合适的内容).
and then use an exception handler to render a custom errorpage (maybe containing a link to a list of meters or whatever is appropriate).
但我不喜欢用许多小的例外来污染我的应用程序.
But I don't like polluting my application with many small exceptions.
另一种可能性是使用 HttpServletResponse
并手动设置状态码:
Another possibility would be using HttpServletResponse
and set the statuscode manually:
@RequestMapping(value = "/{number}", method = RequestMethod.GET)
public String getMeterDetails(@PathVariable("number") final Long number, final Model model,
final HttpServletResponse response) {
final Meter meter = meterService.findOne(number);
if (meter == null) {
response.setStatus(HttpStatus.NOT_FOUND.value());
return "meters/notfound";
}
model.addAttribute("meter", meter);
return "meters/details";
}
但使用此解决方案,我必须为许多控制器方法(如编辑、删除)复制前 5 行.
But with this solution I have to duplicate the first 5 lines for many controller methods (like edit, delete).
有没有一种优雅的方法可以防止多次重复这些行?
Is there an elegant way to prevent duplicating these lines many times?
推荐答案
解决方案比想象的要简单得多.可以使用一个通用的 ResourceNotFoundException
定义如下:
The solution is much simpler than thought. One can use one generic ResourceNotFoundException
defined as follows:
public class ResourceNotFoundException extends RuntimeException { }
然后可以使用 ExceptionHandler
注释处理每个控制器中的错误:
then one can handle errors within every controller with an ExceptionHandler
annotation:
class MeterController {
// ...
@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handleResourceNotFoundException() {
return "meters/notfound";
}
// ...
@RequestMapping(value = "/{number}/edit", method = RequestMethod.GET)
public String viewEdit(@PathVariable("number") final Meter meter,
final Model model) {
if (meter == null) throw new ResourceNotFoundException();
model.addAttribute("meter", meter);
return "meters/edit";
}
}
每个控制器都可以为ResourceNotFoundException
定义自己的ExceptionHandler
.
Every controller can define its own ExceptionHandler
for the ResourceNotFoundException
.
这篇关于Spring MVC:如何返回自定义 404 错误页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!