Is there at spring boot a configuration possible, which returns all errors in a json format?
For example 404, or 401. Need to replace this 404 page with just json.
Many thanks
Is there at spring boot a configuration possible, which returns all errors in a json format?
For example 404, or 401. Need to replace this 404 page with just json.
Many thanks
This is since by default, springboot produces the error as html. To get the Json output, add produces argument as follows so that the content returned will be for sure in json format.
@RequestMapping(....., produces = "application/json")
You can custom your error controller to handle it:
@RestController
public class CustomErrorController extends BasicErrorController {
private final Logger logger = LoggerFactory.getLogger(CustomErrorController.class);
public CustomErrorController(ErrorAttributes errorAttributes) {
super(errorAttributes, new ErrorProperties());
}
// let all MediaType return json data
@RequestMapping(consumes = MediaType.ALL_VALUE)
public ResponseEntity<Map<String, Object>> allError(HttpServletRequest request, HttpServletResponse response) {
return super.error(request);
}
}
You mean using @ControllerAdvice
with the content header set to application/json