docs(0): [java]-[wcferry-mvn]-创建通用返回类及返回码

This commit is contained in:
chandler 2024-09-25 22:17:07 +08:00
parent 5a5a300e43
commit 66d6655f50
7 changed files with 3199 additions and 4557 deletions

View File

@ -0,0 +1,24 @@
package com.iamteer.controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.iamteer.entity.TResponse;
import com.iamteer.enums.ResponseCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping("/test")
@Api(tags = "测试-接口")
public class TestController {
@ApiOperation(value = "测试", notes = "index")
@PostMapping(value = "/index")
public TResponse<Object> index() {
return TResponse.ok(ResponseCodeEnum.SUCCESS, "");
}
}

View File

@ -0,0 +1,18 @@
package com.iamteer.entity;
/**
* 返回类接口
*/
public interface IResponse {
/**
* 状态码
*/
String getCode();
/**
* 返回信息
*/
String getMsg();
}

View File

@ -0,0 +1,111 @@
package com.iamteer.entity;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.iamteer.enums.ResponseCodeEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;
/**
* 返回类封装
*/
@Data
@ToString
@Accessors(chain = true)
public class TResponse<T> {
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/**
* 状态码
*/
@ApiModelProperty(value = "状态码")
private String code;
/**
* 返回信息
*/
@ApiModelProperty(value = "返回信息")
private String msg;
/**
* 响应时间
*/
@ApiModelProperty(value = "响应时间")
private String time;
/**
* 响应数据
*/
@ApiModelProperty(value = "响应数据")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private T data;
/**
* 返回类
*
* @author chandler
* @date 2023/4/5 11:31
* @param t 返回码类
* @param data 返回数据
* @return TResponse对象
*/
public TResponse(IResponse t, T data) {
this(t);
this.data = data;
}
/**
* 返回类
*
* @author chandler
* @date 2023/4/5 11:31
* @param t 返回码类
* @param msg 返回信息
* @return TResponse对象
*/
public TResponse(IResponse t, String msg) {
this.code = t.getCode();
this.msg = msg;
this.time = LocalDateTime.now().format(FORMATTER);
}
/**
* 返回类
*
* @author chandler
* @date 2023/4/5 11:31
* @param t 返回码类
* @param msg 返回信息
* @return TResponse对象
*/
public TResponse(IResponse t, T data, String msg) {
this(t, data);
// 重写返回信息-替换默认的信息
this.msg = msg;
}
public TResponse(IResponse t) {
this.code = t.getCode();
this.msg = t.getMsg();
this.time = LocalDateTime.now().format(FORMATTER);
}
public static <T> TResponse<T> ok(IResponse t) {
return new TResponse<>(t);
}
public static <T> TResponse<T> ok(IResponse t, T data) {
return new TResponse<>(t, data);
}
public static <T> TResponse<T> fail(String msg) {
return new TResponse<>(ResponseCodeEnum.FAILED, msg);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,3 @@
# Ignore everything in this directory
*
# Except this file !.gitkeep

View File

@ -0,0 +1,3 @@
# Ignore everything in this directory
*
# Except this file !.gitkeep

View File

@ -0,0 +1,65 @@
package com.iamteer.enums;
import com.iamteer.entity.IResponse;
/**
* 枚举-返回类状态码
*/
public enum ResponseCodeEnum implements IResponse {
/**
* 成功-200
*/
SUCCESS("200", "请求成功"),
/**
* 参数错误-400
*/
PARAM_ERROR("400", "参数错误"),
/**
* 401-身份验证失败
*/
NO_AUTH("401", "身份验证失败"),
/**
* 403-您无权访问此资源
*/
UNAUTHORIZED("403", "您无权访问此资源"),
/**
* 404-未找到该资源
*/
NOT_FOUND("404", "未找到该资源"),
/**
* 失败-500
*/
FAILED("500", "请求失败"),
;
private final String code;
private final String msg;
ResponseCodeEnum(String code, String msg) {
this.code = code;
this.msg = msg;
}
@Override
public String getCode() {
return code;
}
@Override
public String getMsg() {
return msg;
}
@Override
public String toString() {
return this.name() + "{" + code + '|' + msg + "}";
}
}