fix(web): 修复开启 i18n 后访问接口报错的问题

This commit is contained in:
2025-02-20 20:36:55 +08:00
parent 18b7ee6dfe
commit 0d7f777fd5
3 changed files with 41 additions and 31 deletions

View File

@@ -21,14 +21,13 @@ import org.apache.commons.lang3.reflect.TypeUtils;
import org.springdoc.core.parsers.ReturnTypeParser; import org.springdoc.core.parsers.ReturnTypeParser;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import top.continew.starter.apidoc.util.DocUtils; import top.continew.starter.apidoc.util.DocUtils;
import top.continew.starter.web.model.R;
import java.lang.reflect.Type; import java.lang.reflect.Type;
/** /**
* SpringDoc 全局响应处理器 * SpringDoc 全局响应处理器
* <p> * <p>
* 接口文档全局添加响应格式 {@link R} * 接口文档全局添加响应格式 {@link com.feiniaojin.gracefulresponse.data.Response}
* </p> * </p>
* *
* @author echo * @author echo

View File

@@ -183,7 +183,7 @@ public class GlobalResponseAutoConfiguration {
@ConditionalOnProperty(prefix = PropertiesConstants.WEB_RESPONSE, name = "i18n", havingValue = "true") @ConditionalOnProperty(prefix = PropertiesConstants.WEB_RESPONSE, name = "i18n", havingValue = "true")
public MessageSource messageSource() { public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames("i18n", "i18n/empty-messages"); messageSource.setBasenames("i18n", "i18n/messages");
messageSource.setDefaultEncoding("UTF-8"); messageSource.setDefaultEncoding("UTF-8");
messageSource.setDefaultLocale(Locale.CHINA); messageSource.setDefaultLocale(Locale.CHINA);
return messageSource; return messageSource;

View File

@@ -18,10 +18,12 @@ package top.continew.starter.web.model;
import cn.hutool.extra.spring.SpringUtil; import cn.hutool.extra.spring.SpringUtil;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.feiniaojin.gracefulresponse.api.ResponseStatusFactory;
import com.feiniaojin.gracefulresponse.data.Response; import com.feiniaojin.gracefulresponse.data.Response;
import com.feiniaojin.gracefulresponse.data.ResponseStatus; import com.feiniaojin.gracefulresponse.data.ResponseStatus;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import top.continew.starter.web.autoconfigure.response.GlobalResponseProperties;
import java.util.Objects;
/** /**
* 响应信息 * 响应信息
@@ -32,16 +34,15 @@ import top.continew.starter.web.autoconfigure.response.GlobalResponseProperties;
@Schema(description = "响应信息") @Schema(description = "响应信息")
public class R<T> implements Response { public class R<T> implements Response {
private static final GlobalResponseProperties PROPERTIES = SpringUtil.getBean(GlobalResponseProperties.class); private static final ResponseStatusFactory RESPONSE_STATUS_FACTORY = SpringUtil
private static final String DEFAULT_SUCCESS_CODE = PROPERTIES.getDefaultSuccessCode(); .getBean(ResponseStatusFactory.class);
private static final String DEFAULT_SUCCESS_MSG = PROPERTIES.getDefaultSuccessMsg(); private static final ResponseStatus DEFAULT_STATUS_SUCCESS = RESPONSE_STATUS_FACTORY.defaultSuccess();
private static final String DEFAULT_ERROR_CODE = PROPERTIES.getDefaultErrorCode(); private static final ResponseStatus DEFAULT_STATUS_ERROR = RESPONSE_STATUS_FACTORY.defaultError();
private static final String DEFAULT_ERROR_MSG = PROPERTIES.getDefaultErrorMsg();
/** /**
* 状态码 * 状态码
*/ */
@Schema(description = "状态码", example = "1") @Schema(description = "状态码", example = "0")
private String code; private String code;
/** /**
@@ -60,7 +61,7 @@ public class R<T> implements Response {
* 时间戳 * 时间戳
*/ */
@Schema(description = "时间戳", example = "1691453288000") @Schema(description = "时间戳", example = "1691453288000")
private final Long timestamp = System.currentTimeMillis(); private Long timestamp;
/** /**
* 响应数据 * 响应数据
@@ -68,29 +69,42 @@ public class R<T> implements Response {
@Schema(description = "响应数据") @Schema(description = "响应数据")
private T data; private T data;
/**
* 状态信息
*/
private ResponseStatus status;
public R() { public R() {
} }
public R(ResponseStatus status) {
this.status = status;
}
public R(String code, String msg) { public R(String code, String msg) {
this.setCode(code); this.setCode(code);
this.setMsg(msg); this.setMsg(msg);
} }
public R(ResponseStatus status, T data) {
this(status);
this.setData(data);
}
public R(String code, String msg, T data) { public R(String code, String msg, T data) {
this(code, msg); this(code, msg);
this.data = data; this.setData(data);
} }
@Override @Override
public void setStatus(ResponseStatus status) { public void setStatus(ResponseStatus status) {
this.setCode(status.getCode()); this.status = status;
this.setMsg(status.getMsg());
} }
@Override @Override
@JsonIgnore @JsonIgnore
public ResponseStatus getStatus() { public ResponseStatus getStatus() {
return null; return status;
} }
@Override @Override
@@ -101,24 +115,23 @@ public class R<T> implements Response {
@Override @Override
@JsonIgnore @JsonIgnore
public Object getPayload() { public Object getPayload() {
return null; return data;
} }
public String getCode() { public String getCode() {
return code; return status.getCode();
} }
public void setCode(String code) { public void setCode(String code) {
this.code = code; status.setCode(code);
this.success = DEFAULT_SUCCESS_CODE.equals(code);
} }
public String getMsg() { public String getMsg() {
return msg; return status.getMsg();
} }
public void setMsg(String msg) { public void setMsg(String msg) {
this.msg = msg; status.setMsg(msg);
} }
public T getData() { public T getData() {
@@ -130,15 +143,11 @@ public class R<T> implements Response {
} }
public boolean isSuccess() { public boolean isSuccess() {
return success; return Objects.equals(DEFAULT_STATUS_SUCCESS.getCode(), status.getCode());
}
public void setSuccess(boolean success) {
this.success = success;
} }
public Long getTimestamp() { public Long getTimestamp() {
return timestamp; return System.currentTimeMillis();
} }
/** /**
@@ -147,7 +156,7 @@ public class R<T> implements Response {
* @return R / * @return R /
*/ */
public static R ok() { public static R ok() {
return new R(DEFAULT_SUCCESS_CODE, DEFAULT_SUCCESS_MSG); return new R(DEFAULT_STATUS_SUCCESS);
} }
/** /**
@@ -157,7 +166,7 @@ public class R<T> implements Response {
* @return R / * @return R /
*/ */
public static R ok(Object data) { public static R ok(Object data) {
return new R(DEFAULT_SUCCESS_CODE, DEFAULT_SUCCESS_MSG, data); return new R(DEFAULT_STATUS_SUCCESS, data);
} }
/** /**
@@ -168,7 +177,9 @@ public class R<T> implements Response {
* @return R / * @return R /
*/ */
public static R ok(String msg, Object data) { public static R ok(String msg, Object data) {
return new R(DEFAULT_SUCCESS_CODE, msg, data); R r = ok(data);
r.setMsg(msg);
return r;
} }
/** /**
@@ -177,7 +188,7 @@ public class R<T> implements Response {
* @return R / * @return R /
*/ */
public static R fail() { public static R fail() {
return new R(DEFAULT_ERROR_CODE, DEFAULT_ERROR_MSG); return new R(DEFAULT_STATUS_ERROR);
} }
/** /**