gpt4 book ai didi

spring boot @Cachable 返回填充了空值的父类(super class)的所有字段

转载 作者:行者123 更新时间:2023-12-05 02:18:01 24 4
gpt4 key购买 nike

我们面临一个奇怪的问题,我一直不明白发生了什么,希望其他人已经遇到同样的问题并且知道发生了什么。

我们使用@Cachable 编写了一个简单的 REST 服务:

@GetMapping(value = "/get/{" + PARAM_TENANT + "}/{" + PARAM_UID + "}")
@Cacheable(value = GET_ORDERS_BY_UID )
public GetOrdersResponseDto getOrdersByUid(@PathVariable final String tenant, @PathVariable final String uid) {
....
return new GetOrdersResponseDto(createCacheKey(), orderResponseDtos);
}

GetOrdersResponseDto 由几个字段组成。有些包含自定义类的实例、它们的一些列表和其他简单的原始值。

当从缓存中提供 GetOrdersResponseDto 响应时,存储在列表中且位于对象父类(super class)中的所有对象字段都将填充空值。

我们使用 hazelcast 作为缓存实现。我们的缓存配置非常基础:

@Component
public class HazelcastConfig extends Config {

@Autowired
public HazelcastConfig(final ConfigClient configClient) {
super();

final GroupConfig groupConfig = getGroupConfig();
final String name = configClient
.getConfigPropertyValueOrThrow("public", "com.orderservice.hazelcast.group.name");
groupConfig.setName("foogroup");

final String password = configClient
.getConfigPropertyValueOrThrow("public", "com.orderservice.hazelcast.group.password");
groupConfig.setPassword(password);

响应类如下所示:

public class GetOrdersResponseDto implements Serializable {

private String cacheSerial;

private List<OrderResponseDto> orderResponseDtos;

}

而且问题只发生在属于 OrderResponseDto 父类(super class)的 OrderResponseDto 字段上。

我希望有人能给我们提示是什么导致了这种奇怪的行为。

编辑:我发现,问题只发生在存储在列表中的对象...

最佳答案

这是 Java 行为。参见 https://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html

如果您的对象是可序列化的并且扩展了一个不可序列化的对象,那么父对象的字段仅被初始化,而不是有用的 NotSerializeException,这就是为什么您将它们作为空值。

您可以在单元测试中证明这一点。这是一个可以重复使用的 - https://github.com/hazelcast/hazelcast-code-samples/blob/master/serialization/hazelcast-airlines/the-code/src/test/java/com/hazelcast/samples/serialization/hazelcast/airlines/V1FlightTest.java

关于spring boot @Cachable 返回填充了空值的父类(super class)的所有字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46406156/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com