Skip to content

Commit 0d7f777

Browse files
committed
fix(web): 修复开启 i18n 后访问接口报错的问题
1 parent 18b7ee6 commit 0d7f777

File tree

3 files changed

+41
-31
lines changed

3 files changed

+41
-31
lines changed

continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/response/ApiDocGlobalResponseHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@
2121
import org.springdoc.core.parsers.ReturnTypeParser;
2222
import org.springframework.core.MethodParameter;
2323
import top.continew.starter.apidoc.util.DocUtils;
24-
import top.continew.starter.web.model.R;
2524

2625
import java.lang.reflect.Type;
2726

2827
/**
2928
* SpringDoc 全局响应处理器
3029
* <p>
31-
* 接口文档全局添加响应格式 {@link R}
30+
* 接口文档全局添加响应格式 {@link com.feiniaojin.gracefulresponse.data.Response}
3231
* </p>
3332
*
3433
* @author echo

continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/response/GlobalResponseAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public GrI18nResponseBodyAdvice grI18nResponseBodyAdvice() {
183183
@ConditionalOnProperty(prefix = PropertiesConstants.WEB_RESPONSE, name = "i18n", havingValue = "true")
184184
public MessageSource messageSource() {
185185
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
186-
messageSource.setBasenames("i18n", "i18n/empty-messages");
186+
messageSource.setBasenames("i18n", "i18n/messages");
187187
messageSource.setDefaultEncoding("UTF-8");
188188
messageSource.setDefaultLocale(Locale.CHINA);
189189
return messageSource;

continew-starter-web/src/main/java/top/continew/starter/web/model/R.java

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
import cn.hutool.extra.spring.SpringUtil;
2020
import com.fasterxml.jackson.annotation.JsonIgnore;
21+
import com.feiniaojin.gracefulresponse.api.ResponseStatusFactory;
2122
import com.feiniaojin.gracefulresponse.data.Response;
2223
import com.feiniaojin.gracefulresponse.data.ResponseStatus;
2324
import io.swagger.v3.oas.annotations.media.Schema;
24-
import top.continew.starter.web.autoconfigure.response.GlobalResponseProperties;
25+
26+
import java.util.Objects;
2527

2628
/**
2729
* 响应信息
@@ -32,16 +34,15 @@
3234
@Schema(description = "响应信息")
3335
public class R<T> implements Response {
3436

35-
private static final GlobalResponseProperties PROPERTIES = SpringUtil.getBean(GlobalResponseProperties.class);
36-
private static final String DEFAULT_SUCCESS_CODE = PROPERTIES.getDefaultSuccessCode();
37-
private static final String DEFAULT_SUCCESS_MSG = PROPERTIES.getDefaultSuccessMsg();
38-
private static final String DEFAULT_ERROR_CODE = PROPERTIES.getDefaultErrorCode();
39-
private static final String DEFAULT_ERROR_MSG = PROPERTIES.getDefaultErrorMsg();
37+
private static final ResponseStatusFactory RESPONSE_STATUS_FACTORY = SpringUtil
38+
.getBean(ResponseStatusFactory.class);
39+
private static final ResponseStatus DEFAULT_STATUS_SUCCESS = RESPONSE_STATUS_FACTORY.defaultSuccess();
40+
private static final ResponseStatus DEFAULT_STATUS_ERROR = RESPONSE_STATUS_FACTORY.defaultError();
4041

4142
/**
4243
* 状态码
4344
*/
44-
@Schema(description = "状态码", example = "1")
45+
@Schema(description = "状态码", example = "0")
4546
private String code;
4647

4748
/**
@@ -60,37 +61,50 @@ public class R<T> implements Response {
6061
* 时间戳
6162
*/
6263
@Schema(description = "时间戳", example = "1691453288000")
63-
private final Long timestamp = System.currentTimeMillis();
64+
private Long timestamp;
6465

6566
/**
6667
* 响应数据
6768
*/
6869
@Schema(description = "响应数据")
6970
private T data;
7071

72+
/**
73+
* 状态信息
74+
*/
75+
private ResponseStatus status;
76+
7177
public R() {
7278
}
7379

80+
public R(ResponseStatus status) {
81+
this.status = status;
82+
}
83+
7484
public R(String code, String msg) {
7585
this.setCode(code);
7686
this.setMsg(msg);
7787
}
7888

89+
public R(ResponseStatus status, T data) {
90+
this(status);
91+
this.setData(data);
92+
}
93+
7994
public R(String code, String msg, T data) {
8095
this(code, msg);
81-
this.data = data;
96+
this.setData(data);
8297
}
8398

8499
@Override
85100
public void setStatus(ResponseStatus status) {
86-
this.setCode(status.getCode());
87-
this.setMsg(status.getMsg());
101+
this.status = status;
88102
}
89103

90104
@Override
91105
@JsonIgnore
92106
public ResponseStatus getStatus() {
93-
return null;
107+
return status;
94108
}
95109

96110
@Override
@@ -101,24 +115,23 @@ public void setPayload(Object payload) {
101115
@Override
102116
@JsonIgnore
103117
public Object getPayload() {
104-
return null;
118+
return data;
105119
}
106120

107121
public String getCode() {
108-
return code;
122+
return status.getCode();
109123
}
110124

111125
public void setCode(String code) {
112-
this.code = code;
113-
this.success = DEFAULT_SUCCESS_CODE.equals(code);
126+
status.setCode(code);
114127
}
115128

116129
public String getMsg() {
117-
return msg;
130+
return status.getMsg();
118131
}
119132

120133
public void setMsg(String msg) {
121-
this.msg = msg;
134+
status.setMsg(msg);
122135
}
123136

124137
public T getData() {
@@ -130,15 +143,11 @@ public void setData(T data) {
130143
}
131144

132145
public boolean isSuccess() {
133-
return success;
134-
}
135-
136-
public void setSuccess(boolean success) {
137-
this.success = success;
146+
return Objects.equals(DEFAULT_STATUS_SUCCESS.getCode(), status.getCode());
138147
}
139148

140149
public Long getTimestamp() {
141-
return timestamp;
150+
return System.currentTimeMillis();
142151
}
143152

144153
/**
@@ -147,7 +156,7 @@ public Long getTimestamp() {
147156
* @return R /
148157
*/
149158
public static R ok() {
150-
return new R(DEFAULT_SUCCESS_CODE, DEFAULT_SUCCESS_MSG);
159+
return new R(DEFAULT_STATUS_SUCCESS);
151160
}
152161

153162
/**
@@ -157,7 +166,7 @@ public static R ok() {
157166
* @return R /
158167
*/
159168
public static R ok(Object data) {
160-
return new R(DEFAULT_SUCCESS_CODE, DEFAULT_SUCCESS_MSG, data);
169+
return new R(DEFAULT_STATUS_SUCCESS, data);
161170
}
162171

163172
/**
@@ -168,7 +177,9 @@ public static R ok(Object data) {
168177
* @return R /
169178
*/
170179
public static R ok(String msg, Object data) {
171-
return new R(DEFAULT_SUCCESS_CODE, msg, data);
180+
R r = ok(data);
181+
r.setMsg(msg);
182+
return r;
172183
}
173184

174185
/**
@@ -177,7 +188,7 @@ public static R ok(String msg, Object data) {
177188
* @return R /
178189
*/
179190
public static R fail() {
180-
return new R(DEFAULT_ERROR_CODE, DEFAULT_ERROR_MSG);
191+
return new R(DEFAULT_STATUS_ERROR);
181192
}
182193

183194
/**

0 commit comments

Comments
 (0)