SpringBoot exception distinguishes between page requests or Json requests

 

@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

 

[plain]  view plain copy  
 
  1. Whitelabel Error Page  
  2.   
  3. This application has no explicit mapping for /error, so you are seeing this as a fallback.  
  4.   
  5. Tue Nov 29 10:48:26 CST 2016  
  6. There was an unexpected error (type=Bad Request, status=400).  
  7. Required String parameter 'fileName' is not present  



Use postman request to return a response in json format

 

[plain]  view plain copy  
 
  1. {  
  2.     "timestamp": 1480388264722,  
  3.     "status": 400,  
  4.     "error": "Bad Request",  
  5.     "exception": "org.springframework.web.bind.MissingServletRequestParameterException",  
  6.     "message": "Required String parameter 'fileName' is not present",  
  7.     "path": "/file/delete"  
  8. }  


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

 

[java]  view plain copy  
 
  1. @RequestMapping(  
  2.         value = {"${error.path:/error}"},  
  3.         produces = {"text/html"}  
  4.     )  
  5.     public ModelAndView errorHtml(HttpServletRequest request) {  
  6.         return new ModelAndView("error"this.getErrorAttributes(request, false));  
  7.     }  
  8.   
  9.     @RequestMapping({"${error.path:/error}"})  
  10.     @ResponseBody  
  11.     public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {  
  12.         Map body = this.getErrorAttributes(request, this.getTraceParameter(request));  
  13.         HttpStatus status = this.getStatus(request);  
  14.         returnnew ResponseEntity(body, status);   
  15.     }  

 

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326523501&siteId=291194637