v1.0.82 #94
@@ -341,6 +341,10 @@ public class MultilingualFieldBinder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean isSimpleValueType(Class<?> clazz) {
|
private boolean isSimpleValueType(Class<?> clazz) {
|
||||||
|
// 泛型字段在反射中表现为 Object,实际值仍需按运行时类型继续遍历
|
||||||
|
if (Object.class == clazz) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if (clazz.isPrimitive() || clazz.isEnum() || clazz.isAnnotation()) {
|
if (clazz.isPrimitive() || clazz.isEnum() || clazz.isAnnotation()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package com.imyeyu.api.util;
|
|||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
import org.springframework.http.server.ServerHttpRequest;
|
import org.springframework.http.server.ServerHttpRequest;
|
||||||
@@ -14,11 +16,15 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
|||||||
/**
|
/**
|
||||||
* 统一处理响应中的多语言字段回填
|
* 统一处理响应中的多语言字段回填
|
||||||
*
|
*
|
||||||
|
* <p>必须在全局返回包装处理器之前执行,否则响应体已被包装为 TimiResponse,
|
||||||
|
* 其 {@code data} 字段声明为泛型 {@code T}(反射类型为 Object),无法继续向下遍历。</p>
|
||||||
|
*
|
||||||
* @author 夜雨
|
* @author 夜雨
|
||||||
* @since 2026-07-17 00:00
|
* @since 2026-07-17 00:00
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
@RestControllerAdvice
|
@RestControllerAdvice
|
||||||
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class MultilingualResponseBodyAdvice implements ResponseBodyAdvice<Object> {
|
public class MultilingualResponseBodyAdvice implements ResponseBodyAdvice<Object> {
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package com.imyeyu.api.util;
|
||||||
|
|
||||||
|
import com.imyeyu.api.modules.common.entity.Multilingual;
|
||||||
|
import com.imyeyu.api.modules.common.entity.Tag;
|
||||||
|
import com.imyeyu.api.modules.common.service.MultilingualService;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.mock.web.MockHttpServletRequest;
|
||||||
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
/// 响应多语言字段回填测试
|
||||||
|
///
|
||||||
|
/// @author 夜雨
|
||||||
|
/// @since 2026-11-26
|
||||||
|
class MultilingualFieldBinderTest {
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void resetRequestContext() {
|
||||||
|
RequestContextHolder.resetRequestAttributes();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 泛型包装对象的 data 字段在反射中表现为 Object,仍需继续向下回填
|
||||||
|
@Test
|
||||||
|
void bindsMultilingualFieldThroughObjectTypedWrapper() {
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
request.addHeader("Accept-Language", "zh-CN");
|
||||||
|
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||||
|
|
||||||
|
MultilingualService service = mock(MultilingualService.class);
|
||||||
|
Multilingual multilingual = new Multilingual();
|
||||||
|
multilingual.setId("lang-1");
|
||||||
|
multilingual.setZhCN("标签");
|
||||||
|
when(service.listByIdList(List.of("lang-1"))).thenReturn(List.of(multilingual));
|
||||||
|
|
||||||
|
Tag tag = new Tag();
|
||||||
|
tag.setNameLangId("lang-1");
|
||||||
|
|
||||||
|
MultilingualFieldBinder binder = new MultilingualFieldBinder(service);
|
||||||
|
binder.bind(new ObjectDataWrapper(tag));
|
||||||
|
|
||||||
|
assertEquals("标签", tag.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 模拟 TimiResponse 等泛型包装对象
|
||||||
|
private record ObjectDataWrapper(Object data) {
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user