我有一个 Spring Boot RESTful API 来接收 SMS 并向客户端发送 SMS。我的应用程序连接到我们的本地短信服务器,并通过移动运营商接收短信并将其推送给客户端。我的应用程序运行良好。但我想通过实现缓存来优化我的应用程序。我正在使用 Spring Boot 的简单缓存。创建新短信时我面临一些挑战。
发送/接收的所有短信均采用对话形式(每票)并附有客户端。所以我很难将客户端保存到缓存中。下面是 createClient() 代码片段:
@Transactional
@Caching(evict = {
@CacheEvict("allClientsPage"),
@CacheEvict("countClients")
}, put = {
@CachePut(value = "clients", key = "#result.id", unless="#result != null"),
@CachePut(value = "clientsByPhone", key = "#result.phoneNumber", unless="#result != null")
})
public Client create(Client client) {
Client c = new Client();
if (client.getName() != null) c.setName(client.getName().trim());
c.setPhoneNumber(client.getPhoneNumber().trim());
/**---***/
c.setCreatedAt(new Date());
return clientRepository.save(c);
}
当我尝试创建新客户端时,
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'id' cannot be found on null
被抛出。
任何帮助将不胜感激。
最佳答案
不要使用unless="# result!= null"
,而是使用condition="#result != null"
关于java - Spring Boot 缓存 SpEL (#result) 返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54985969/