Tag - Spring

Spring REST Error Handling Example

In this article, we will show you error handling in Spring Boot REST application. Technologies used : Spring Boot 2.1.2.RELEASESpring 5.1.4.RELEASEMaven 3Java 8 1. /error 1.1 By default, Spring Boot provides a BasicErrorController controller for /error mapping that handles all errors, and getErrorAttributes to produce a JSON response with details of the error, the HTTP status, and the exception message. { "timestamp":"2019-02-27T04:03:52.398+0000", "status":500, "error":"Internal Server Error", "message":"...", "path":"/path" } BasicErrorController.java package org.springframework.boot.autoconfigure.web.servlet.error; //... @Controller @RequestMapping("${server.error.path:${error.path:/error}}") public class BasicErrorController extends AbstractErrorController { //... @RequestMapping public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) { Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL)); ...

Read more...