spring boot @Cachable返回填充空值的所有超类字段

时间:2017-09-25 13:13:09

标签: spring spring-mvc spring-boot hazelcast

我们正面临一个奇怪的问题而且我不会理解最新情况并希望其他人已经有同样的问题并且知道发生了什么。

我们编写了一个使用@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响应时,存储在列表中的对象的所有字段都位于对象中,超类用空值填充。

我们使用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的字段中,这些字段是OrderResponseDto的超类的一部分。

我希望有人可以告诉我们这种奇怪行为的原因是什么。

编辑:我发现问题只出现在列表中的对象...

1 个答案:

答案 0 :(得分:3)

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

如果你的对象是可序列化的并扩展了一个不可序列化的对象,那么代替那些有用的NotSerializeException,只会初始化父对象的字段,这就是为什么你把它们作为空值。< / p>

您可以在单元测试中证明这一点。 这是重复使用的问题 - 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