一、创建返回对象工具类
package com.yxh.demo.demo.util;
import com.yxh.demo.demo.model.ReturnModel;
public class ReturnModelUtil {
/**
* 请求成功返回参数
* @param object
* @return
*/
public static ReturnModel success(Object object){
//此处写死,正常业务到时可以设置是否有无分页返回
return new ReturnModel(200,"成功",object);
}
/**
* 请求失败返回参数
* @param code
* @param msg
* @return
*/
public static ReturnModel fail(int code,String msg){
return new ReturnModel(code,msg,null);
}
}
二、创建返回对象
package com.yxh.demo.demo.model;
public class ReturnModel<T> {
public int code;
public String msg;
public T data;
public ReturnModel(int code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
}
三、自定义异常类
package com.yxh.demo.demo.exception;
public class MyException extends RuntimeException {
private int code;
private String msg;
public MyException(int code, String msg) {
this.code = code;
this.msg = msg;
}
public MyException(String message, int code, String msg) {
super(message);
this.code = code;
this.msg = msg;
}
public MyException(String message, Throwable cause, int code, String msg) {
super(message, cause);
this.code = code;
this.msg = msg;
}
public MyException(Throwable cause, int code, String msg) {
super(cause);
this.code = code;
this.msg = msg;
}
public MyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, int code, String msg) {
super(message, cause, enableSuppression, writableStackTrace);
this.code = code;
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
四、设置请求拦截器抛异常
package com.yxh.demo.demo.inter;
import com.yxh.demo.demo.exception.MyException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ReqInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("Interceptor preHandler method is running !");
if(true){
throw new MyException(1000,"登陆异常");
}
request.setAttribute("token","123456");
return super.preHandle(request, response, handler);
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("Interceptor postHandler method is running !");
super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("Interceptor afterCompletion method is running !");
super.afterCompletion(request, response, handler, ex);
}
@Override
public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("Interceptor afterConcurrentHandlingStarted method is running !");
super.afterConcurrentHandlingStarted(request, response, handler);
}
}
五、统一异常处理
package com.yxh.demo.demo.exception;
import com.alibaba.fastjson.JSONObject;
import com.yxh.demo.demo.model.ReturnModel;
import com.yxh.demo.demo.util.ReturnModelUtil;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.util.List;
@RestControllerAdvice
public class SysException {
@ExceptionHandler(Exception.class)
public ReturnModel exceptionHandler(Exception e) {
if (e instanceof MyException) {
return ReturnModelUtil.fail(((MyException) e).getCode(),((MyException) e).getMsg());
}else if (e instanceof ValidationException) {
return ReturnModelUtil.fail(1002,e.getMessage());
} else{
return ReturnModelUtil.fail(500,e.getMessage());
}
}
}
这是一个请求拦截器,拦截指定的资源,我是设置拦截/api/下的所有资源,
访问http://localhost:8080/api/test
游览器看到的结果
下一篇讲解Springboot自定义拦截器