通用类型处理
新增: - BaseResponse - ErrorCode - ResultUtil 修改: - UserController
This commit is contained in:
parent
b14d21cb58
commit
158f70751a
|
@ -1,7 +1,16 @@
|
||||||
package com.jsl.oa.controllers;
|
package com.jsl.oa.controllers;
|
||||||
|
|
||||||
|
import com.jsl.oa.utils.BaseResponse;
|
||||||
|
import com.jsl.oa.utils.ErrorCode;
|
||||||
|
import com.jsl.oa.utils.ResultUtil;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class UserController {
|
public class UserController {
|
||||||
|
|
||||||
|
@PostMapping("/user/register")
|
||||||
|
public BaseResponse userRegister() {
|
||||||
|
return ResultUtil.error(ErrorCode.WRONG_PASSWORD);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
27
src/main/java/com/jsl/oa/utils/BaseResponse.java
Normal file
27
src/main/java/com/jsl/oa/utils/BaseResponse.java
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
package com.jsl.oa.utils;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
|
public class BaseResponse {
|
||||||
|
private final String output;
|
||||||
|
private final Integer code;
|
||||||
|
private final String message;
|
||||||
|
private final Object data;
|
||||||
|
|
||||||
|
public BaseResponse(String output, Integer code, String message, Object data) {
|
||||||
|
this.output = output;
|
||||||
|
this.code = code;
|
||||||
|
this.message = message;
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BaseResponse(String output, Integer code, String message) {
|
||||||
|
this.output = output;
|
||||||
|
this.code = code;
|
||||||
|
this.message = message;
|
||||||
|
this.data = null;
|
||||||
|
}
|
||||||
|
}
|
17
src/main/java/com/jsl/oa/utils/ErrorCode.java
Normal file
17
src/main/java/com/jsl/oa/utils/ErrorCode.java
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
package com.jsl.oa.utils;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public enum ErrorCode {
|
||||||
|
WRONG_PASSWORD("WrongPassword", 40010, "密码错误");
|
||||||
|
|
||||||
|
private final String output;
|
||||||
|
private final Integer code;
|
||||||
|
private final String message;
|
||||||
|
ErrorCode(String output, Integer code, String message) {
|
||||||
|
this.output = output;
|
||||||
|
this.code = code;
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
}
|
20
src/main/java/com/jsl/oa/utils/ResultUtil.java
Normal file
20
src/main/java/com/jsl/oa/utils/ResultUtil.java
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
package com.jsl.oa.utils;
|
||||||
|
|
||||||
|
public class ResultUtil {
|
||||||
|
|
||||||
|
public static BaseResponse success() {
|
||||||
|
return new BaseResponse("Success", 200, "操作成功", null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BaseResponse success(Object data) {
|
||||||
|
return new BaseResponse("Success", 200, "操作成功", data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BaseResponse error(ErrorCode errorCode) {
|
||||||
|
return new BaseResponse(errorCode.getOutput(), errorCode.getCode(), errorCode.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BaseResponse error(ErrorCode errorCode, Object data) {
|
||||||
|
return new BaseResponse(errorCode.getOutput(), errorCode.getCode(), errorCode.getMessage(), data);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user