DTO parameter verification and unified exception handling

Commonly used annotation application cases in DTO files
SpringBoot parameter verification
Dependency package

<!-- After springBoot2.3 version, you need to introduce javax.validation yourself -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

Annotations in dto class

@Data
public class OrderDTO {<!-- -->
  /**
   * Site selection; comma separated
   */
  @NotBlank
  private String stations;
  /**
   * Feature selection; comma separated
   */
  @NotBlank
  private String elements;
  /**
   * Service time
   */
  @NotNull
  private Integer serveDuration;
  /**
   *Interface ID
   */
  @NotBlank
  private String interfaceId;
  /**
   * Product ID
   */
  @NotBlank
  private String dataCode;
}

Add @Valid annotation to controller parameters

/**
* Create Order
 * @param orderDTO the order parameter object passed in by the front end
 * @return result
 */
@PreAuthorize("hasAuthority('user:order:create')")
@PostMapping("/create")
public Object create(@RequestBody @Validated OrderDTO orderDTO) {<!-- -->
  return orderService.createOrder(orderDTO);
}

Handle validate exception handling in the global exception handling class

import cma.sxqxgxw.utils.result.CodeMsg;
import cma.sxqxgxw.utils.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.validation.UnexpectedTypeException;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {<!-- -->
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result<?> handleException(Exception e) throws Exception {<!-- -->
        if (e instanceof AccessDeniedException) {<!-- -->
            throw e;
        }
        if (e instanceof HttpRequestMethodNotSupportedException) {<!-- -->
            return Result.error(new CodeMsg(5052, e.getMessage()));
        }
        if (e instanceof ServiceException) {<!-- -->
            return Result.error(new CodeMsg(5052, e.getMessage()));
        }
        if (e instanceof HttpMessageNotReadableException) {<!-- -->
            return Result.error(new CodeMsg(5053, "The parameter cannot be recognized! Please check the parameter"));
        }
        if (e instanceof MissingServletRequestParameterException) {<!-- -->
            return Result.error(CodeMsg.CONTROL_ERROR);
        }
        if (e instanceof UnexpectedTypeException) {<!-- -->
            return Result.error(CodeMsg.CONTROL_ERROR);
        }
        if (e instanceof MaxUploadSizeExceededException) {<!-- -->
            return Result.error(new CodeMsg(5054, "The total file size cannot exceed 300MB"));
        }
        //Method parameter verification exception handling
        if (e instanceof MethodArgumentNotValidException) {<!-- -->
            return Result.error(CodeMsg.CONTROL_ERROR);
        }
        if (e instanceof BindException) {<!-- -->
            return Result.error(CodeMsg.CONTROL_ERROR);
        }
        e.printStackTrace();
        return Result.error(CodeMsg.SERVER_ERROR);
    }
}

Custom return code

public class CodeMsg {<!-- -->
    private int code;
    private String msg;

    //General error code
    public static CodeMsg SUCCESS = new CodeMsg(0, "success");
    public static CodeMsg TOKEN_EXPIRED = new CodeMsg(400, "token expired");
    public static CodeMsg TOKEN_INVALID = new CodeMsg(401, "token parsing exception");
    public static CodeMsg USER_NOT_LOGGED_IN = new CodeMsg(402, "Not logged in, please log in!");
    public static CodeMsg SIGNATURE_ERROR = new CodeMsg(506, "Signature failed");
    public static CodeMsg SERVER_ERROR = new CodeMsg(500, "Server-side exception");
    public static CodeMsg FILE_ERROR = new CodeMsg(501, "File does not exist");
    public static CodeMsg FILE_EXIST = new CodeMsg(5012, "File already exists");
    public static CodeMsg TIME_ERROR = new CodeMsg(502, "The time format is wrong, it should be yyyy-MM-dd HH:mm:ss");
    public static CodeMsg TIMEMISS_ERROR = new CodeMsg(503, "The query time is missing, it should be yyyy-MM-dd HH:mm:ss");
    public static CodeMsg BIND_ERROR = new CodeMsg(504, "Parameter binding exception");
    public static CodeMsg CONTROL_ERROR = new CodeMsg(505, "Method parameter verification failed");
    public static CodeMsg BINDING_FAILED = new CodeMsg(506, "Binding failed");
    public static CodeMsg UNBIND_FAILED = new CodeMsg(507, "Unbinding failed");
    //Omit other custom error codes
    
    private CodeMsg() {<!-- -->
    }
    public CodeMsg(int code, String msg) {<!-- -->
        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;
    }
    /**
     * Returns the error code with parameters
     * @param args
     * @return
     */
    public CodeMsg fillArgs(Object... args) {<!-- -->
        int code = this.code;
        String message = String.format(this.msg, args);
        return new CodeMsg(code, message);
    }
    @Override
    public String toString() {<!-- -->
        return "CodeMsg [code=" + code + ", msg=" + msg + "]";
    }
}

Encapsulation return value and return encoding

import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import java.util.stream.Collectors;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> {<!-- -->
    private int code;
    private String msg="success";
    private T data;
    /**
     * Called when successful
     * */
    public static <T> Result<T> success() {<!-- -->
        Result<T> result = new Result<T>();
        result.code = 1;
        return result;
    }
    public static<T> Result<T> success(T data){<!-- -->
        return new Result<T>(data);
    }
    /**
     * Called when failure occurs
     * */
    public static <T> Result<T> error(CodeMsg codeMsg){<!-- -->
        return new Result<T>(codeMsg);
    }
    private Result(T data) {<!-- -->
        this.data = data;
    }
    private Result(int code, String msg) {<!-- -->
        this.code = code;
        this.msg = msg;
    }
    private Result() {<!-- -->
    }
    private Result(CodeMsg codeMsg) {<!-- -->
        if(codeMsg != null) {<!-- -->
            this.code = codeMsg.getCode();
            this.msg = codeMsg.getMsg();
        }
    }
    /**
     * BindingResult unified processing
     */
    public static Result resolveBindResult(BindingResult bindingResult){<!-- -->
        StringBuilder stringBuilder = new StringBuilder();
        for (String s : bindingResult.getFieldErrors().stream().map(FieldError::getDefaultMessage).collect(Collectors.toList())) {<!-- -->
            stringBuilder.append(",").append(s);
        }
        return Result.error(new CodeMsg(502,stringBuilder.toString().substring(1)));
    }
    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;
    }
    public T getData() {<!-- -->
        return data;
    }
    public void setData(T data) {<!-- -->
        this.data = data;
    }
    @Override
    public String toString() {<!-- -->
        return "Result [code=" + code + ", msg=" + msg + ", data=" + data + "]";
    }
}