2017-09-25 46 views
0

我们正面临一个奇怪的问题,我不放弃了解发生了什么,并希望其他人已经有相同的问题,并有线索发生了什么。spring boot @Cachable返回填充空值的超类的所有字段

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

@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响应从缓存中提供存储在列表AND内的对象的所有字段都位于对象超类中时,将填充空值。

我们使用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领域的问题。

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

编辑:我发现,这个问题只发生为存储里面列出的对象......

+0

你还可以共享GetOrdersResponseDto类吗?而你的意思是'这个问题只发生在存储在列表中的对象上。'? –

+0

添加了代码并试图澄清事情。现在解决方案(因为我们需要一个快速)是为我们正在使用的dtos删除(共享)超类的使用... – xstring

+0

@xstring你曾经以你最初想要的方式工作吗? (GetOrdersResponseDto包含其他对象的列表)。这应该起作用,并且如果没有多一点样本可能有助于找出问题。由于'GetOrdersResponseDto'是'Serializable',所以它里面的所有东西都需要也是 –

回答

1

这是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

+0

谢谢你的回答! – xstring

+0

不客气!最后很明显,但花了一段时间才找到。 –