需求说明
使用AOP定义一个统一的返回结构,使得项目规范化,能够自定义异常说明
接口返回结构
import io. swagger. annotations. ApiModel;
import io. swagger. annotations. ApiModelProperty;
import lombok. Data;
@Data
@ApiModel ( "返回说明" )
public class Result < T> {
@ApiModelProperty ( "返回码" )
private int code;
@ApiModelProperty ( "描述" )
private String msg;
@ApiModelProperty ( "数据" )
private T data;
public Result ( ) {
this . setCode ( ResultCode. SUCCESS. val ( ) ) ;
this . setMsg ( ResultCode. SUCCESS. msg ( ) ) ;
}
public Result ( ResultCode code) {
this . setCode ( ResultCode. SUCCESS. val ( ) ) ;
this . setMsg ( ResultCode. SUCCESS. msg ( ) ) ;
}
public Result ( T data) {
this . setCode ( ResultCode. SUCCESS. val ( ) ) ;
this . setMsg ( ResultCode. SUCCESS. msg ( ) ) ;
this . setData ( data) ;
}
public Result ( ResultCode code, String message) {
this . setCode ( code. val ( ) ) ;
this . setMsg ( message) ;
}
public Result ( ResultCode code, String message, T data) {
this . setCode ( code. val ( ) ) ;
this . setMsg ( message) ;
this . setData ( data) ;
}
@Override
public String toString ( ) {
return "ReturnVO{" +
"code='" + code + '\'' +
", message='" + msg + '\'' +
", data=" + data +
'}' ;
}
}
返回值说明
public enum ResultCode {
SUCCESS ( 200 , "成功" ) ,
FAIL ( 500 , "操作失败" ) ,
NullpointerException ( 500 , "空指针异常" ) ,
TokenExpire ( 500 , "token失效 " ) ;
ResultCode ( int value, String msg) {
this . val = value;
this . msg = msg;
}
public int val ( ) {
return val;
}
public String msg ( ) {
return msg;
}
private int val;
private String msg;
}
AOP正题
import org. aspectj. lang. ProceedingJoinPoint;
import org. aspectj. lang. annotation. Around;
import org. aspectj. lang. annotation. Aspect;
import org. aspectj. lang. annotation. Pointcut;
import org. springframework. stereotype. Component;
@Aspect
@Component
public class ResponseAop {
@Pointcut ( "execution(public * com.husscess.qxwgh.controller..*(..))" )
public void httpResponse ( ) {
}
@Around ( "httpResponse()" )
public Result handlerController ( ProceedingJoinPoint proceedingJoinPoint) {
Result result = new Result ( ) ;
try {
Object proceed = proceedingJoinPoint. proceed ( ) ;
if ( proceed instanceof Result ) {
result = ( Result) proceed;
} else {
result. setData ( proceed) ;
}
} catch ( Throwable throwable) {
result = handlerException ( throwable) ;
}
return result;
}
private Result handlerException ( Throwable throwable) {
Result result = new Result ( ) ;
String errorName = throwable. toString ( ) ;
errorName = errorName. substring ( errorName. lastIndexOf ( "." ) + 1 ) ;
result. setMsg ( errorName) ;
result. setCode ( 500 ) ;
return result;
}
}
测试接口
@ResponseBody
@RequestMapping ( "/login" )
@ApiOperation ( value = "登录" , httpMethod = "GET" )
@ApiImplicitParams ( {
@ApiImplicitParam ( name = "username" , value = "账号" , required = true , dataType = "String" , paramType = "query" ) ,
@ApiImplicitParam ( name = "password" , value = "密码" , required = true , dataType = "String" , paramType = "query" ) }
)
public Object login ( String username, String password) {
List< User> list= userService. verifyUser ( username, password) ;
if ( list. size ( ) == 0 ) {
throw new APIException ( "账号或密码错误" ) ;
}
User user= list. get ( 0 ) ;
String tokenWithUserInfos= JWTUtil. createTokenWithUserInfos ( user. getId ( ) , user. getName ( ) , user. getUnit ( ) ,
user. getLevel ( ) , 24 * 60 * 60 L) ;
user. setToken ( tokenWithUserInfos) ;
if ( tokenWithUserInfos != null) {
String sub_jwt = tokenWithUserInfos. substring ( tokenWithUserInfos. lastIndexOf ( "." ) + 1 ) ;
redisUtils. set ( sub_jwt, tokenWithUserInfos, 3 * 60 * 60 ) ;
}
return user;
}
结果返回
目前存在问题
接口返回得用object才行,但是使用object swagger就没法看到返回值说明了,等以后找到方法了再更新
详细说明博客