通用类型处理

新增:
- BaseResponse
- ErrorCode
- ResultUtil

修改:
- UserController
This commit is contained in:
筱锋xiao_lfeng 2023-12-20 21:04:26 +08:00
parent b14d21cb58
commit 158f70751a
Signed by: XiaoLFeng
GPG Key ID: F693AA12AABBFA87
4 changed files with 73 additions and 0 deletions

View File

@ -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);
}
} }

View 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;
}
}

View 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;
}
}

View 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);
}
}