Spring Cloud Spring Boot mybatis distributed micro-service Cloud architecture returns JSON format

Source: Internet
Author: User
Tags error handling

In the example above, @ControllerAdvice mapping to different error handling pages by unifying the different exception is defined. And when we want to implement the RESTful API, the error returned is JSON-formatted data, not HTML pages, which we can easily support.

Essentially, @ExceptionHandler @ResponseBody you can convert the contents of the return of the handler function into JSON format simply by adding it later.

The following is a concrete example to implement exception handling that returns JSON format.

    • Create a unified JSON return object, code: Message type, message: Content, URL: Requested url,data: Request returned data
 Public classErrorinfo<t> {         Public Static FinalInteger OK = 0;  Public Static FinalInteger ERROR = 100; PrivateInteger Code; PrivateString message; PrivateString URL; PrivateT data; //omit getter and setter  }  

Create a custom exception to experiment with catching the exception and return the JSON

 Public class extends Exception {        public  myexception (String message) {          Super(message);      }        }  

ControllerAdd JSON mappings, throw MyException exceptions

@Controller    Public class Hellocontroller {        @RequestMapping ("/json")      publicthrows  myexception {          thrownew myexception ("Error 2 occurred");      }    }  

MyExceptioncreate a corresponding processing for an exception

@ControllerAdvice Public classGlobalexceptionhandler {@ExceptionHandler (value= MyException.class) @ResponseBody PublicErrorinfo<string> Jsonerrorhandler (httpservletrequest req, myexception e)throwsException {errorinfo<String> r =NewErrorinfo<>();          R.setmessage (E.getmessage ());          R.setcode (Errorinfo.error); R.setdata ("Some Data");          R.seturl (Req.getrequesturl (). toString ()); returnR; }    }  

Launch app, Access: Http://localhost:8080/json, you can get the following return content:

{      "Some Data",      "error 2"      , "Http://localhost:8080/json"         }  

Source source technical support for complete projects 1791743380

Spring Cloud Spring Boot mybatis distributed micro-service Cloud architecture returns JSON format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.