add
This commit is contained in:
parent
5d30d38821
commit
85e40bf56d
33
spring-boot/paramsCheck/.gitignore
vendored
Normal file
33
spring-boot/paramsCheck/.gitignore
vendored
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
HELP.md
|
||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### STS ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
59
spring-boot/paramsCheck/pom.xml
Normal file
59
spring-boot/paramsCheck/pom.xml
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.5.5</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
<groupId>com.fanxb</groupId>
|
||||||
|
<artifactId>exceptionTest</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>exceptionTest</name>
|
||||||
|
<description>Demo project for Spring Boot</description>
|
||||||
|
<properties>
|
||||||
|
<java.version>11</java.version>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-validation</artifactId>
|
||||||
|
<version>2.5.5</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<version>${project.parent.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<exclude>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
</exclude>
|
||||||
|
</excludes>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.fanxb.exceptiontest;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class ExceptionTestApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(ExceptionTestApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.fanxb.exceptiontest.config;
|
||||||
|
|
||||||
|
import com.fanxb.exceptiontest.entity.Result;
|
||||||
|
import com.fanxb.exceptiontest.entity.exception.BaseException;
|
||||||
|
import com.fanxb.exceptiontest.entity.exception.CustomBusinessException;
|
||||||
|
import com.fasterxml.jackson.databind.ser.Serializers;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-09-24-下午4:37
|
||||||
|
*/
|
||||||
|
@RestControllerAdvice
|
||||||
|
@Slf4j
|
||||||
|
public class ExceptionHandle {
|
||||||
|
@ExceptionHandler(Exception.class)
|
||||||
|
public Result handleException(Exception e) {
|
||||||
|
BaseException be;
|
||||||
|
if (e instanceof BaseException) {
|
||||||
|
be = (BaseException) e;
|
||||||
|
//手动抛出的异常,仅记录message
|
||||||
|
log.info("baseException:{}", be.getMessage());
|
||||||
|
if (be instanceof CustomBusinessException) {
|
||||||
|
//可在这里进行一些针对特定异常的处理逻辑
|
||||||
|
log.info("customBusinessException:{}", be.getMessage());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//其它异常,非自动抛出的,无需给前端返回具体错误内容(用户不需要看见空指针之类的异常信息)
|
||||||
|
log.error("other exception:{}", e.getMessage(), e);
|
||||||
|
be = new BaseException("系统异常,请稍后重试", e);
|
||||||
|
}
|
||||||
|
return new Result(be.getCode(), be.getMessage(), null);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,68 @@
|
|||||||
|
package com.fanxb.exceptiontest.controller;
|
||||||
|
|
||||||
|
import com.fanxb.exceptiontest.entity.Result;
|
||||||
|
import com.fanxb.exceptiontest.entity.consistant.Insert;
|
||||||
|
import com.fanxb.exceptiontest.entity.consistant.Update;
|
||||||
|
import com.fanxb.exceptiontest.entity.exception.BaseException;
|
||||||
|
import com.fanxb.exceptiontest.entity.exception.CustomBusinessException;
|
||||||
|
import com.fanxb.exceptiontest.entity.vo.TestBody;
|
||||||
|
import com.fanxb.exceptiontest.entity.vo.TestBody2;
|
||||||
|
import com.fanxb.exceptiontest.entity.vo.TestBody3;
|
||||||
|
import org.hibernate.validator.constraints.Range;
|
||||||
|
import org.springframework.validation.BindException;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import javax.validation.constraints.Email;
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-09-24-下午4:46
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@Validated
|
||||||
|
public class TestController {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* url参数校验
|
||||||
|
*/
|
||||||
|
@GetMapping("/test1")
|
||||||
|
public Result test1(@NotBlank @RequestParam String param1, @Range(max = 100, min = 10) @RequestParam int param2) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 表单校验
|
||||||
|
*/
|
||||||
|
@PostMapping("/test2")
|
||||||
|
public Result test2(@Valid @RequestBody TestBody body) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 嵌套校验
|
||||||
|
*/
|
||||||
|
@PostMapping("/test3")
|
||||||
|
public Result test3(@Valid @RequestBody TestBody2 testBody2) {
|
||||||
|
throw new RuntimeException("我是test2");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分组校验1
|
||||||
|
*/
|
||||||
|
@PostMapping("/test4")
|
||||||
|
public Result test4(@Validated({Update.class}) @RequestBody TestBody3 testBody) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分组校验2
|
||||||
|
*/
|
||||||
|
@PostMapping("/test5")
|
||||||
|
public Result test5(@Validated({Insert.class}) @RequestBody TestBody3 testBody) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
package com.fanxb.exceptiontest.entity;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.hibernate.validator.constraints.Range;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
|
||||||
|
import javax.validation.constraints.Email;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-09-24-下午4:39
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class Result implements Serializable {
|
||||||
|
private static final long serialVersionUID = 451834802206432869L;
|
||||||
|
/**
|
||||||
|
* 1:成功,其他失败,具体数值在自定义的异常类中
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
@Email
|
||||||
|
@Range
|
||||||
|
private int code;
|
||||||
|
/**
|
||||||
|
* message提示内容,当接口异常时提示异常信息
|
||||||
|
*/
|
||||||
|
private String message;
|
||||||
|
/**
|
||||||
|
* 携带真正需要返回的数据
|
||||||
|
*/
|
||||||
|
private Object data;
|
||||||
|
|
||||||
|
public static Result success(Object obj) {
|
||||||
|
return new Result(1, null, obj);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.fanxb.exceptiontest.entity.consistant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-09-28-下午5:05
|
||||||
|
*/
|
||||||
|
public interface Insert {
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.fanxb.exceptiontest.entity.consistant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-09-28-下午5:05
|
||||||
|
*/
|
||||||
|
public interface Update {
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
package com.fanxb.exceptiontest.entity.exception;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-09-24-下午5:13
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
public class BaseException extends RuntimeException {
|
||||||
|
private static final long serialVersionUID = -3149747381632839680L;
|
||||||
|
/**
|
||||||
|
* 基本错误code为0
|
||||||
|
*/
|
||||||
|
private static final int BASE_CODE = 0;
|
||||||
|
private final int code;
|
||||||
|
private final String message;
|
||||||
|
|
||||||
|
public BaseException() {
|
||||||
|
this(null, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BaseException(String message) {
|
||||||
|
this(message, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BaseException(Exception e) {
|
||||||
|
this(null, null, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BaseException(String message, Exception e) {
|
||||||
|
this(message, null, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected BaseException(String message, Integer code, Exception e) {
|
||||||
|
super(e);
|
||||||
|
this.message = message == null ? "" : message;
|
||||||
|
this.code = code == null ? BASE_CODE : code;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMessage() {
|
||||||
|
if (this.message != null && this.message.length() > 0) {
|
||||||
|
return this.message;
|
||||||
|
}
|
||||||
|
return super.getMessage();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.fanxb.exceptiontest.entity.exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-09-26-上午10:41
|
||||||
|
*/
|
||||||
|
public class CustomBusinessException extends BaseException {
|
||||||
|
private static final long serialVersionUID = 1564935267302330109L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义业务异常错误码
|
||||||
|
*/
|
||||||
|
private static final int ERROR_CODE = -1;
|
||||||
|
|
||||||
|
public CustomBusinessException() {
|
||||||
|
super("自定义义务异常", ERROR_CODE, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CustomBusinessException(String message, Exception e) {
|
||||||
|
super(message, ERROR_CODE, e);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.fanxb.exceptiontest.entity.vo;
|
||||||
|
|
||||||
|
import com.fanxb.exceptiontest.entity.consistant.Insert;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.validation.constraints.Email;
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-09-28-下午4:29
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class TestBody {
|
||||||
|
@NotBlank
|
||||||
|
@Email(message = "请输入一个邮箱")
|
||||||
|
private String param1;
|
||||||
|
@NotNull
|
||||||
|
private String param2;
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.fanxb.exceptiontest.entity.vo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import javax.validation.constraints.Email;
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-09-28-下午4:29
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class TestBody2 {
|
||||||
|
@NotBlank
|
||||||
|
@Email(message = "请输入一个邮箱")
|
||||||
|
private String param1;
|
||||||
|
@NotNull
|
||||||
|
private String param2;
|
||||||
|
@NotNull
|
||||||
|
@Valid
|
||||||
|
private TestBody testBody;
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.fanxb.exceptiontest.entity.vo;
|
||||||
|
|
||||||
|
import com.fanxb.exceptiontest.entity.consistant.Insert;
|
||||||
|
import com.fanxb.exceptiontest.entity.consistant.Update;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import javax.validation.constraints.Email;
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-09-28-下午4:29
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class TestBody3 {
|
||||||
|
@NotBlank(groups = {Insert.class})
|
||||||
|
private String param1;
|
||||||
|
@NotBlank(groups = {Update.class})
|
||||||
|
private String param2;
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
|
Loading…
x
Reference in New Issue
Block a user