@RestController
public class RestErrorEndpoint implements ErrorController {
@Override
public String getErrorPath () {
return null;
}
@RequestMapping(value = {"${server.error.path:${error.path:/error}}"},produces="application/json")
@ResponseBody
public String error(HttpServletRequest request) {
System.out.println("json");
return "helloError";
}
@RequestMapping( value = {"${server.error.path:${error.path:/error}}"}, produces = {"text/html"} )
public ModelAndView errorHtml(HttpServletRequest request) {
System.out.println("html");
return new ModelAndView("/error/error");
}
// @RequestMapping({"${error.path:/error}"})
// @ResponseBody
// public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
// Map body = this.getErrorAttributes(request, this.getTraceParameter(request));
// HttpStatus status = this.getStatus(request);
// return new ResponseEntity(body, status);
// }
}
Spring boot outputs error responses in different formats according to the content of the Accept header. For example, html pages are generated for browser requests, and json-formatted returns are generated for other requests. The field is judged by the content of text/html of accept.
produces = {"text/html"} page requests.
produces="application/json" for json or ajax requests
============================================================
Recently started to use springboot, found a strange phenomenon, a url error
Use the browser address request to return an html interface
- Whitelabel Error Page
- This application has no explicit mapping for /error, so you are seeing this as a fallback.
- Tue Nov 29 10:48:26 CST 2016
- There was an unexpected error (type=Bad Request, status=400).
- Required String parameter 'fileName' is not present
Use postman request to return a response in json format
- {
- "timestamp": 1480388264722,
- "status": 400,
- "error": "Bad Request",
- "exception": "org.springframework.web.bind.MissingServletRequestParameterException",
- "message": "Required String parameter 'fileName' is not present",
- "path": "/file/delete"
- }
How does spring boot distinguish data in different formats returned by this request? After a step by step follow up. The essence is on the BasicErrorController class
- @RequestMapping(
- value = {"${error.path:/error}"},
- produces = {"text/html"}
- )
- public ModelAndView errorHtml(HttpServletRequest request) {
- return new ModelAndView("error", this.getErrorAttributes(request, false));
- }
- @RequestMapping({"${error.path:/error}"})
- @ResponseBody
- public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
- Map body = this.getErrorAttributes(request, this.getTraceParameter(request));
- HttpStatus status = this.getStatus(request);
- returnnew ResponseEntity(body, status);
- }
Spring boot outputs error responses in different formats according to the content of the Accept header. For example, html pages are generated for browser requests, and json-formatted returns are generated for other requests. The content of text/html whose field is accept is judged