feat: 处理返回long类型数据处理

This commit is contained in:
wyl
2022-12-01 21:49:26 +08:00
parent cf1d2cdd3a
commit b154e57aef
5 changed files with 100 additions and 32 deletions
@@ -19,7 +19,11 @@ public class SecurityUserInfo {
*/
private String username = "未知";
/**
* 用户账号
* 登录账号
*/
private String account = "";
/**
* 用户id
*/
private Long id = -1L;
/**
@@ -2,7 +2,6 @@ package com.aos.common.model.result;
import com.aos.common.model.ResultCodeInterface;
import lombok.AllArgsConstructor;
import org.slf4j.MDC;
@AllArgsConstructor
@SuppressWarnings("unchecked")
@@ -12,43 +11,65 @@ public enum WebResponse {
*/
WebResponse;
public <T> WebResult ok() {
return new WebResult(ResultCode.SUCCEED);
public <T> WebResult buildWebResult(ResultCodeInterface resultCode, T data, String traceId) {
return new WebResult(resultCode, data, traceId);
}
public <T> WebResult ok(ResultCodeInterface resultCode, T data) {
return new WebResult(resultCode, data);
return this.buildWebResult(resultCode, data, null);
}
public <T> WebResult ok(ResultCodeInterface resultCode, String traceId) {
return this.buildWebResult(resultCode, null, traceId);
}
public <T> WebResult ok(ResultCodeInterface resultCode) {
return this.buildWebResult(resultCode, null, null);
}
public <T> WebResult ok(T data) {
return new WebResult(ResultCode.SUCCEED, data);
return this.buildWebResult(ResultCode.SUCCEED, data, null);
}
public <T> WebResult error() {
return new WebResult(ResultCode.FAILED);
public <T> WebResult ok(String traceId) {
return this.buildWebResult(ResultCode.SUCCEED, null, traceId);
}
public <T> WebResult error(T data) {
return new WebResult(ResultCode.FAILED, data);
public <T> WebResult ok(T data, String traceId) {
return this.buildWebResult(ResultCode.SUCCEED, data, traceId);
}
public <T> WebResult ok() {
return this.ok(ResultCode.SUCCEED, null);
}
public <T> WebResult error(ResultCodeInterface resultCode, T data) {
return new WebResult(resultCode, data);
return this.buildWebResult(resultCode, data, null);
}
public <T> WebResult error(ResultCodeInterface resultCode, String traceId) {
return this.buildWebResult(resultCode, null, traceId);
}
public <T> WebResult error(ResultCodeInterface resultCode) {
return new WebResult(resultCode);
return this.buildWebResult(resultCode, null, null);
}
public <T> WebResult error(String code, String msg) {
return new WebResult(code, msg);
public <T> WebResult error(T data) {
return this.buildWebResult(ResultCode.FAILED, data, null);
}
private <T> WebResult WebResponse(String code, String msg, T data) {
return new WebResult(code, msg, data);
public <T> WebResult error(String traceId) {
return this.buildWebResult(ResultCode.FAILED, null, traceId);
}
private <T> WebResult WebResponse(String code, String msg) {
return new WebResult(code, msg);
public <T> WebResult error(T data, String traceId) {
return this.buildWebResult(ResultCode.FAILED, data, traceId);
}
public <T> WebResult error() {
return this.ok(ResultCode.FAILED, null);
}
}
@@ -36,10 +36,6 @@ public class WebResult<T> {
}
public WebResult(String code, String msg, T data) {
this(code, msg, data, null);
}
public WebResult(String code, String msg, T data, String traceId) {
this.code = code;
this.msg = msg;
@@ -47,16 +43,36 @@ public class WebResult<T> {
this.traceId = traceId;
}
public WebResult(ResultCodeInterface resultCode) {
this(resultCode, null);
public WebResult(String code, String msg, T data) {
this(code, msg, data, null);
}
public WebResult(ResultCodeInterface resultCode, T data) {
this(resultCode.getCode(), resultCode.getMsg(), data);
public WebResult(String code, String msg, String traceId) {
this(code, msg, null, traceId);
}
public WebResult(String code, String msg) {
this(code, msg, null);
this(code, msg, null, null);
}
public WebResult(String code, T data) {
this(code, null, data, null);
}
public WebResult(ResultCodeInterface resultCode, T data, String traceId) {
this(resultCode.getCode(), resultCode.getMsg(), data, traceId);
}
public WebResult(ResultCodeInterface resultCode, T data) {
this(resultCode.getCode(), resultCode.getMsg(), data, null);
}
public WebResult(ResultCodeInterface resultCode, String traceId) {
this(resultCode, null, traceId);
}
public WebResult(ResultCodeInterface resultCode) {
this(resultCode, null, null);
}
}
@@ -5,9 +5,7 @@ import com.alibaba.ttl.TtlRunnable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
@@ -47,4 +45,16 @@ public class TestController {
public void test4() throws Exception {
throw new Exception();
}
@GetMapping("/test5/{id}")
@ApiOperation(value = "test5")
public void test5(@PathVariable("id") String id) {
System.out.println(id);
}
@GetMapping("/test5/test1")
@ApiOperation(value = "test6")
public void test6() {
System.out.println(111);
}
}
@@ -4,11 +4,17 @@ import com.aos.common.model.exception.AosBaseBizException;
import com.aos.common.model.result.WebResponse;
import com.aos.common.model.result.WebResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.Optional;
/**
* @author: wangyl
* @date: 2022/11/14
@@ -25,7 +31,12 @@ public class ControllerErrorResponseAdvice {
return WebResponse.WebResponse.error(ex.getErrorCode(), ex.getMessage());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.OK)
public WebResult<?> handleException(MethodArgumentNotValidException ex) {
log.error("MethodArgumentNotValidException", ex);
return this.handleError(ex.getBindingResult());
}
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.OK)
@@ -33,4 +44,10 @@ public class ControllerErrorResponseAdvice {
log.error("Exception", ex);
return WebResponse.WebResponse.error();
}
private WebResult<?> handleError(BindingResult bindingResult) {
FieldError fieldError = bindingResult.getFieldError();
String message = Optional.ofNullable(fieldError).map(DefaultMessageSourceResolvable::getDefaultMessage).orElse("参数不正确");
return WebResponse.WebResponse.error(message);
}
}