使用idea搭建springboot项目
按照如下步骤进行创建
学习自 https://juejin.im/post/5e7ab0bae51d45271b749815
1.引入相关依赖,按照自己项目实际情况进行引入
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--所有的springboot工程都必须继承spring-boot-starter-parent-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<groupId>com.xxx</groupId>
<artifactId>springboot-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--web功能的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--lombok依赖包,简化类。-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
2.创建springboot启动项
@SpringBootApplication
public class MySpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringbootApplication.class);
}
}
3.编写全局自定义响应类(引入了Swagger)
@Getter
@ApiModel
public class ResultVO<T> {
@ApiModelProperty(value = "状态码", notes = "默认1000是成功")
private int code;
@ApiModelProperty(value = "响应信息", notes = "来说明响应情况")
private String msg;
@ApiModelProperty(value = "响应的具体数据")
private T data;
public ResultVO(T data) {
this(ResultCode.SUCCESS, data);
}
public ResultVO(ResultCode resultCode, T data) {
this.code = resultCode.getCode();
this.msg = resultCode.getMsg();
this.data = data;
}
}
4.使用枚举定义状态码
@Getter
public enum ResultCode {
SUCCESS(1000, "操作成功"),
FAILED(1001, "响应失败"),
VALIDATE_FAILED(1002, "参数校验失败"),
ERROR(5000, "未知错误");
private int code;
private String msg;
ResultCode(int code, String msg) {
this.code = code;
this.msg = msg;
}
}
5.全局异常处理
@RestControllerAdvice
public class ExceptionControllerAdvice {
@ExceptionHandler(APIException.class)
public ResultVO<String> APIExceptionHandler(APIException e) {
return new ResultVO<>(ResultCode.FAILED, e.getMsg());
}
}
**自定义异常**
@Getter //只要getter方法,无需setter
public class APIException extends RuntimeException {
private int code;
private String msg;
public APIException() {
this(1001, "接口错误");
}
public APIException(String msg) {
this(1001, msg);
}
public APIException(int code, String msg) {
super(msg);
this.code = code;
this.msg = msg;
}
}
6.自定义参数校验
@Data
@ApiModel("用户")
public class User {
@ApiModelProperty("用户id")
@NotNull(message = "用户id不能为空")
private Long id;
@ApiModelProperty("用户账号")
@NotNull(message = "用户账号不能为空")
@Size(min = 6, max = 11, message = "账号长度必须是6-11个字符")
private String account;
@ApiModelProperty("用户密码")
@NotNull(message = "用户密码不能为空")
@Size(min = 6, max = 11, message = "密码长度必须是6-16个字符")
private String password;
@ApiModelProperty("用户邮箱")
@NotNull(message = "用户邮箱不能为空")
@Email(message = "邮箱格式不正确")
private String email;
}
**请求需要添加 @Valid 注解**
@ApiOperation("添加用户")
@PostMapping("/addUser")
public String addUser(@RequestBody @Valid User user) {
return userService.addUser(user);
}