【发布时间】:2020-07-25 16:54:31
【问题描述】:
我想为带有消息的错误代码实现自定义错误页面。 到目前为止,按照 baeldung 指南,我在后端得到了这个;
自定义异常:
public class TicketNotFoundException extends RuntimeException
{
public TicketNotFoundException(Long id)
{
super("Ticket not found with id: "+id);
}
}
自定义响应:
public class CustomErrorResponse
{
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh:mm:ss")
private LocalDateTime timestamp;
private int status;
private String error;
//getters setters
}
自定义异常处理程序:
@ControllerAdvice
public class CustomGlobalExceptionHandler extends ResponseEntityExceptionHandler
{
@ExceptionHandler(value = TicketNotFoundException.class)
public ResponseEntity<CustomErrorResponse> customHandleNotFound(Exception ex)
{
CustomErrorResponse errors = new CustomErrorResponse();
errors.setTimestamp(LocalDateTime.now());
errors.setError(ex.getMessage());
errors.setStatus(HttpStatus.NOT_FOUND.value());
return new ResponseEntity<>(errors, HttpStatus.NOT_FOUND);
}
}
并且响应本身有效:
{ 时间戳:“2020-04-13 09:33:52”,状态:404,错误:“票不是 找到 id: 1" }
后端终端:
Resolved [com.eggorko.ebt.ticket.TicketNotFoundException: Ticket not found with id: 1]
所以我的问题是我应该在客户端做什么?
客户端控制器如下所示:
@GetMapping("/{id}")
public String ticket(@PathVariable Long id, Model model)
{
String url = "http://localhost:8080/api/ticket/";
ResponseEntity<Ticket> ticket = restTemplate.getForEntity( url+ id, Ticket.class);
model.addAttribute("ticket",ticket.getBody());
model.addAttribute("title","Tickets");
return "ticket";
}
我做了一点客户端,但它不起作用:
@Controller
public class MyErrorController implements ErrorController
{
@RequestMapping("/error")
public String handleError(HttpServletRequest request)
{
Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
if (status != null) {
Integer statusCode = Integer.valueOf(status.toString());
if(statusCode == HttpStatus.NOT_FOUND.value()) {
return "error-404";
}
else if(statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
return "error-500";
}
}
return "error";
}
@Override
public String getErrorPath()
{
return "/error";
}
}
这是我在客户端的终端中得到的:
2020-04-13 10:34:40.559 ERROR 12868 --- [nio-8081-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException$NotFound: 404 : [{"timestamp":"2020-04-13 10:34:40","status":404,"error":"Ticket not found with id: 1"}]] with root cause
它会转到 500 错误页面,而不是 404。
我有点理解为什么它不起作用。我在客户端没有任何处理错误,它进入错误 500。但我不知道如何处理它。
更新
所以我这样做了:
String url = "http://localhost:8080/api/ticket/";
try {
ResponseEntity<Ticket> ticket = restTemplate.getForEntity(url + id, Ticket.class);
model.addAttribute("ticket",ticket.getBody());
model.addAttribute("title","Tickets");
}catch (Exception e)
{
String msg = e.getMessage();
model.addAttribute("message",msg);
return "/error";
}
return "ticket";
现在至少我得到了一个错误页面,其中包含来自后端的实际消息。但是这个解决方案正在解决 MyErrorController。 MyErrorController 不会触发并且基本上已过时。
【问题讨论】:
标签: java spring spring-boot rest error-handling