补充接口统一返回和统一异常返回
							parent
							
								
									182b10d055
								
							
						
					
					
						commit
						851d04e639
					
				| @ -0,0 +1,21 @@ | ||||
| package org.ils.file.exception; | ||||
| 
 | ||||
| import org.ils.file.utils.Result; | ||||
| 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; | ||||
| 
 | ||||
| /** | ||||
|  * 统一异常类 | ||||
|  * @author chenjf | ||||
|  */ | ||||
| @ControllerAdvice | ||||
| public class GlobalExceptionHandler { | ||||
|     @ExceptionHandler(value = Exception.class) | ||||
|     @ResponseBody | ||||
|     public Result handleCustomException(HttpServletRequest request, Exception e) { | ||||
|         return Result.error(e.getMessage()); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,151 @@ | ||||
| package org.ils.file.utils; | ||||
| 
 | ||||
| import io.swagger.annotations.ApiModelProperty; | ||||
| import lombok.Data; | ||||
| 
 | ||||
| import java.io.Serializable; | ||||
| 
 | ||||
| /** | ||||
|  * 接口返回数据格式 | ||||
|  * @author chenjf | ||||
|  * @date 2023/07/14 | ||||
|  * @param <T> | ||||
|  */ | ||||
| @Data | ||||
| public class Result <T> implements Serializable { | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|     @ApiModelProperty(value = "成功标志") | ||||
|     private boolean success = true; | ||||
| 
 | ||||
| 
 | ||||
|     @ApiModelProperty(value = "返回处理消息") | ||||
|     private String message = ""; | ||||
| 
 | ||||
| 
 | ||||
|     @ApiModelProperty(value = "返回代码") | ||||
|     private Integer code = CommonConstant.HTTP_CODE_200; | ||||
| 
 | ||||
| 
 | ||||
|     @ApiModelProperty(value = "返回数据对象") | ||||
|     private T result; | ||||
| 
 | ||||
| 
 | ||||
|     @ApiModelProperty(value = "时间戳") | ||||
|     private long timestamp = System.currentTimeMillis(); | ||||
| 
 | ||||
|     public Result() { | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * | ||||
|      * @param code 响应code | ||||
|      * @param message 消息 | ||||
|      */ | ||||
|     public Result(Integer code, String message) { | ||||
|         this.code = code; | ||||
|         this.message = message; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * | ||||
|      * @param message 消息 | ||||
|      */ | ||||
|     public Result<T> success(String message) { | ||||
|         this.message = message; | ||||
|         this.code = CommonConstant.HTTP_CODE_200; | ||||
|         this.success = true; | ||||
|         return this; | ||||
|     } | ||||
| 
 | ||||
|     public static<T> Result<T> ok() { | ||||
|         Result<T> r = new Result<T>(); | ||||
|         r.setSuccess(true); | ||||
|         r.setCode(CommonConstant.HTTP_CODE_200); | ||||
|         return r; | ||||
|     } | ||||
| 
 | ||||
|     public static<T> Result<T> ok(String msg) { | ||||
|         Result<T> r = new Result<T>(); | ||||
|         r.setSuccess(true); | ||||
|         r.setCode(CommonConstant.HTTP_CODE_200); | ||||
|         //Result OK(String msg)方法会造成兼容性问题 issues/I4IP3D
 | ||||
|         r.setResult((T) msg); | ||||
|         r.setMessage(msg); | ||||
|         return r; | ||||
|     } | ||||
| 
 | ||||
|     public static<T> Result<T> ok(T data) { | ||||
|         Result<T> r = new Result<T>(); | ||||
|         r.setSuccess(true); | ||||
|         r.setCode(CommonConstant.HTTP_CODE_200); | ||||
|         r.setResult(data); | ||||
|         return r; | ||||
|     } | ||||
| 
 | ||||
|     public static<T> Result<T> OK() { | ||||
|         Result<T> r = new Result<T>(); | ||||
|         r.setSuccess(true); | ||||
|         r.setCode(CommonConstant.HTTP_CODE_200); | ||||
|         return r; | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     public static<T> Result<T> OK(String msg) { | ||||
|         Result<T> r = new Result<T>(); | ||||
|         r.setSuccess(true); | ||||
|         r.setCode(CommonConstant.HTTP_CODE_200); | ||||
|         r.setMessage(msg); | ||||
|         r.setResult((T) msg); | ||||
|         return r; | ||||
|     } | ||||
| 
 | ||||
|     public static<T> Result<T> OK(T data) { | ||||
|         Result<T> r = new Result<T>(); | ||||
|         r.setSuccess(true); | ||||
|         r.setCode(CommonConstant.HTTP_CODE_200); | ||||
|         r.setResult(data); | ||||
|         return r; | ||||
|     } | ||||
| 
 | ||||
|     public static<T> Result<T> OK(String msg, T data) { | ||||
|         Result<T> r = new Result<T>(); | ||||
|         r.setSuccess(true); | ||||
|         r.setCode(CommonConstant.HTTP_CODE_200); | ||||
|         r.setMessage(msg); | ||||
|         r.setResult(data); | ||||
|         return r; | ||||
|     } | ||||
| 
 | ||||
|     public static<T> Result<T> error(String msg, T data) { | ||||
|         Result<T> r = new Result<T>(); | ||||
|         r.setSuccess(false); | ||||
|         r.setCode(CommonConstant.HTTP_CODE_201); | ||||
|         r.setMessage(msg); | ||||
|         r.setResult(data); | ||||
|         return r; | ||||
|     } | ||||
| 
 | ||||
|     public static<T> Result<T> error(String msg) { | ||||
|         return error(CommonConstant.HTTP_CODE_201, msg); | ||||
|     } | ||||
| 
 | ||||
|     public static<T> Result<T> error(int code, String msg) { | ||||
|         Result<T> r = new Result<T>(); | ||||
|         r.setCode(code); | ||||
|         r.setMessage(msg); | ||||
|         r.setSuccess(false); | ||||
|         return r; | ||||
|     } | ||||
| 
 | ||||
|     public Result<T> error500(String message) { | ||||
|         this.message = message; | ||||
|         this.code = CommonConstant.HTTP_CODE_500; | ||||
|         this.success = false; | ||||
|         return this; | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
					Loading…
					
					
				
		Reference in New Issue