Exception Handling (a) SpringBoot to add a unified API

First, I put disorders fall into two types, one is controlled, or discovered by the conditions are not right we take the initiative to throw exceptions, like the city before the chestnut number does not exist; the other is uncontrollable, or bug is caused by abnormal procedures, but this anomaly did not want to give the front end of metamorphosis direct throw a 500 exception.

Steps are as follows:

Step 1, a new class Exception

RESTException create a new class in actively recruiting an exception, throw a RESTException class instance. It contains two attributes, code and message. code to throw an exception code is represented by http status codes, message is the message you want to tell the front, such as "city number does not exist" and the like.

/ ** 
 * the Name: RESTException 
 * the Description: abnormality information 
 * / 
public  class DescribeException the extends Exception { 
int code; // status code String Message; // exception message public int the getCode () { return code; } public void setCode ( int code) { the this .code = code; } @Override public String the getMessage () { return Message; } public void setMessage(String message) { this.message = message; } }

Step 2, an abnormal build a splicer

Create an exception handler, it has two exception handling, a processing initiative thrown exception, a non-active handling exceptions.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Name:APIExceptionHandler
 * Description:
 */
@ControllerAdvice
public class APIExceptionHandler {
Logger logger
LoggerFactory.getLogger = ( the this .getClass ()); / ** * active processing service discovery issues thrown exception * @param Request * @param E * @return * @throws Exception * / @ExceptionHandler (value = DescribeException. Class ) @ResponseBody public ResponseEntity <DescribeException> baseErrorHandler (the HttpServletRequest Request, DescribeException E) throws Exception { // outputs the error to the log logger.error ( "DescribeException Handler --- Host: {} invokes url: {} eRROR: {}", request.getRemoteHost(), request.getRequestURL(), e.getMessage()); return new ResponseEntity<DescribeException>(e, HttpStatus.valueOf(e.getCode())); } /** * 系统抛出的没有处理过的异常 * @param request * @param e * @return * @throws Exception */ @ExceptionHandler(value = Exception.class) @ResponseBody public ResponseEntity<Exception> defaultErrorHandler(HttpServletRequest request, Exception e) throws Exception { //The output of the error to the log logger.error ( "the Host --- DefaultException Handler: {} Invokes URL: ERROR {}: {}" , request.getRemoteHost (), request.getRequestURL (), e.getMessage ()); return new new ResponseEntity <Exception> ( new new Exception ( "Soory, server like jerking off, programmer small rescue partners are crazy!" ), HttpStatus.INTERNAL_SERVER_ERROR); } }

The main exception handling exception message transmission service to the front end processing, exception processing method of the inactive return an unusual unified message to the front end.

Step 3, throw custom exception in the business

Thow new new DescribeException (HttpStatus.INTERNAL_SERVER_ERROR.value (), "Custom Error Messages");

 

Guess you like

Origin www.cnblogs.com/rinack/p/11317077.html