diff --git a/spring-boot-common/src/main/java/com/module/common/api/ResponseResult.java b/spring-boot-common/src/main/java/com/module/common/api/ResponseResult.java new file mode 100644 index 0000000..7d9e54a --- /dev/null +++ b/spring-boot-common/src/main/java/com/module/common/api/ResponseResult.java @@ -0,0 +1,125 @@ +package com.module.common.api; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +/** + * @Description + * @Author lvxc + * @Date 2021/4/26 10:54 + **/ +public class ResponseResult implements Serializable { + + private static final long serialVersionUID = 1L; + + /** + * 状态码 + **/ + private int code; + /** + * 返回信息 + **/ + private String message; + /** + * 返回具体对象信息 + **/ + private T data; + + private String tid; + + public int getCode() { + return code; + } + + public ResponseResult setCode(int code) { + this.code = code; + return this; + } + + public String getMessage() { + return message; + } + + public ResponseResult setMessage(String message) { + this.message = message; + return this; + } + + public T getData() { + return data; + } + + public ResponseResult setData(T data) { + this.data = data; + return this; + } + + public String getTid() { + return tid; + } + + public ResponseResult setTid(String tid) { + this.tid = tid; + return this; + } + + public ResponseResult() { + } + + public ResponseResult(int code) { + this.code = code; + } + + public ResponseResult(int code, String message) { + this.message = message; + this.code = code; + } + + public ResponseResult(int code, String message, T data) { + this.message = message; + this.code = code; + this.data = data; + } + + public static ResponseResult success() { + return new ResponseResult<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg()); + } + + public static ResponseResult success(T data) { + if (data == null) { + List a = new ArrayList<>(); + data = (T) a; + } + return new ResponseResult<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(),data); + } + + public static ResponseResult error(ResultEnum resultEnum) { + ResponseResult result = new ResponseResult<>(); + result.setCode(resultEnum.getCode()); + result.setMessage(resultEnum.getMsg()); + return result; + } + + public static ResponseResult error(Integer code, String msg) { + return new ResponseResult<>(code, msg); + } + + public static ResponseResult error() { + return ResponseResult.error(ResultEnum.FAILED); + } + + public static ResponseResult error(ResultEnum resultEnum, String msg) { + return error(resultEnum.getCode(), msg); + } + + public boolean isSuccess() { + return Objects.equals(this.code, ResultEnum.SUCCESS.getCode()); + } + + public boolean isError() { + return !Objects.equals(this.code, ResultEnum.SUCCESS.getCode()); + } + +} diff --git a/spring-boot-common/src/main/java/com/module/common/api/ResultEnum.java b/spring-boot-common/src/main/java/com/module/common/api/ResultEnum.java new file mode 100644 index 0000000..65b1de3 --- /dev/null +++ b/spring-boot-common/src/main/java/com/module/common/api/ResultEnum.java @@ -0,0 +1,26 @@ +package com.module.common.api; + +public enum ResultEnum { + SUCCESS(200, "成功"), + FAILED(500, "操作失败"), + VALIDATE_FAILED(404, "参数检验失败"), + UNAUTHORIZED(401, "暂未登录或token已经过期"), + FORBIDDEN(403, "没有相关权限"); + + private Integer code; + + private String message; + + ResultEnum(Integer code, String message) { + this.code = code; + this.message = message; + } + + public Integer getCode() { + return this.code; + } + + public String getMsg() { + return this.message; + } +} diff --git a/spring-boot-web/build.gradle b/spring-boot-web/build.gradle index 759d0f5..172a217 100644 --- a/spring-boot-web/build.gradle +++ b/spring-boot-web/build.gradle @@ -14,6 +14,7 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter' + implementation 'org.springframework.boot:spring-boot-starter-web' testImplementation 'org.springframework.boot:spring-boot-starter-test' compile project(':spring-boot-common') } diff --git a/spring-boot-web/src/main/java/com/module/web/controller/TestController.java b/spring-boot-web/src/main/java/com/module/web/controller/TestController.java new file mode 100644 index 0000000..504b948 --- /dev/null +++ b/spring-boot-web/src/main/java/com/module/web/controller/TestController.java @@ -0,0 +1,19 @@ +package com.module.web.controller; + +import com.module.common.api.ResponseResult; +import com.module.web.vo.TestVo; +import org.springframework.web.bind.annotation.*; + +/** + * @Description + * @Author lvxc + * @Date 2021/4/26 10:45 + **/ +@RestController +@RequestMapping("/test") +public class TestController { + @PostMapping(value = "/insert") + public ResponseResult insert(@RequestBody TestVo vo) { + return ResponseResult.success("你好"); + } +} diff --git a/spring-boot-web/src/main/java/com/module/web/vo/TestVo.java b/spring-boot-web/src/main/java/com/module/web/vo/TestVo.java new file mode 100644 index 0000000..f739050 --- /dev/null +++ b/spring-boot-web/src/main/java/com/module/web/vo/TestVo.java @@ -0,0 +1,11 @@ +package com.module.web.vo; + +import java.io.Serializable; + +/** + * @Description + * @Author lvxc + * @Date 2021/4/26 10:50 + **/ +public class TestVo implements Serializable { +} diff --git a/spring-boot-web/src/main/resources/application.yaml b/spring-boot-web/src/main/resources/application.yaml new file mode 100644 index 0000000..226708a --- /dev/null +++ b/spring-boot-web/src/main/resources/application.yaml @@ -0,0 +1,2 @@ +server: + port: 80 \ No newline at end of file