public class Msg {
private int code;
private String msg;
private Map < String , Object > extend = new HashMap < > ( ) ;
public static Msg success ( ) {
Msg result = new Msg ( ) ;
result. setCode ( 100 ) ;
result. setMsg ( "处理成功" ) ;
return result;
}
public static Msg fail ( ) {
Msg result = new Msg ( ) ;
result. setCode ( 200 ) ;
result. setMsg ( "处理失败" ) ;
return result;
}
public Msg add ( String key, Object value) {
this . getExtend ( ) . put ( key, value) ;
return this ;
}
}
import org. springframework. web. bind. annotation. ControllerAdvice ;
import org. springframework. web. bind. annotation. ExceptionHandler ;
import org. springframework. web. bind. annotation. RestControllerAdvice ;
@RestControllerAdvice
public class PrejectExceptionAdvice {
@ExceptionHandler
public Msg doException ( Exception ex) {
ex. printStackTrace ( ) ;
return Msg . fail ( ) . add ( "message" , "服务器故障,请稍后再试!" ) ;
}
}
import lombok. Data ;
@Data
public class R {
private boolean flag;
private Object data;
private String msg;
public R ( ) {
}
public R ( boolean flag) {
this . flag = flag;
}
public R ( boolean flag, Object data) {
this . flag = flag;
this . data = data;
}
public R ( boolean flag, String msg) {
this . flag = flag;
this . msg = msg;
}
public R ( String msg) {
this . msg = msg;
}
}
import org. springframework. web. bind. annotation. ControllerAdvice ;
import org. springframework. web. bind. annotation. ExceptionHandler ;
import org. springframework. web. bind. annotation. RestControllerAdvice ;
@RestControllerAdvice
public class PrejectExceptionAdvice {
@ExceptionHandler
public R doException ( Exception ex) {
ex. printStackTrace ( ) ;
return new R ( "服务器故障,请稍后再试!" ) ;
}
}