You need to know the following few notes
1. @ControllerAdvice: configure the controller notified
by @ControllerAdvice annotation for global controller may be disposed in the same position
annotation method class may be used @ControllerAdvice @ ExceptionHandler, @ InitBinder, @ ModelAttribute annotation to the method.
@ExceptionHandler: a global process controllers in the abnormality.
@InitBinder: used to set WebDataBinder, for automatic reception of request parameters to bind the Model.
@ModelAttribute: Originally role is to bind key-value pair to the Model, here let global @RequestMapping have access to the key set here for
@ControllerAdvice annotation role in all annotated method @RequestMapping controller on .
@ControllerAdvice (Annotations = RestController.class ) all point to the controller annotated @RestController
2. @ResponseBody: @ResponseBody is acting on the process, @ ResponseBody means return the process results in direct writing HTTP response body, @RequestMapping after use, the return value is typically resolved as the jump path, but with @ResponseBody after returning a result not be interpreted as the jump path, but directly written in the HTTP response body. Such as asynchronous get json data, after adding @ResponseBody, it will return json data directly. @RequestBody the insertion process the HTTP request body, using a suitable HttpMessageConverter write request an object body.
3. @ExceptionHandler: a unitary exception types, thereby reducing the complexity of the code repetition rate and
4. @ResponseStatus notes in two ways, one is loaded on a custom exception class, one is added to the target method
that we first talk about class plus in this case the target method, notes there are two parameters , value property abnormal status code, reaseon description is abnormal,
if only return a status code to indicate, it is best not to add reason attribute.
If you add attributes reason, and reason is not "" and the code> 0 (even if the status code is 200), the current request will go error handling.
Exception information entity
@Data
public class ApiResult implements Serializable {
/**
* @Fields serialVersionUID :
*/
private static final long serialVersionUID = -1450204351804859591L;
private ApiResult() {};
public static ApiResult newInstance() {
return new ApiResult();
}
private String msg;
private boolean flag;
private Object object;
}
Construction of abnormal return information
public final class ApiResultGenerator {
public static ApiResult result(boolean flag, String msg, Object object, Throwable throwable) {
ApiResult apiResult = ApiResult.newInstance();
apiResult.setFlag(flag);
apiResult.setMsg(msg == "" ? "success" : msg);
apiResult.setObject(object);
return apiResult;
}
public static ApiResult successResult(Object object) {
return result(true, "", object, null);
}
public static ApiResult errorApiResult(String msg, Throwable throwable) {
return result(false, msg, "", throwable);
}
}
Intercept abnormal in all RestController
//@ControllerAdvice注解是用来配置控制器通知的
//@ControllerAdvice的annotations属性值为RestController.class,
//也就是只有添加了@RestController注解的控制器才会进入全局异常处理
@ControllerAdvice(annotations = RestController.class)
@ResponseBody
public class RestExceptionHandler {
//来配置需要拦截的异常类型,默认是全局类型
@ExceptionHandler
//配置遇到该异常后返回数据时的StatusCode的值
@ResponseStatus
public ApiResult runTimeExceptionHandler(Exception exception) {
return ApiResultGenerator.errorApiResult(exception.getMessage(), exception);
}
}