java - Spring Boot 缓存 SpEL (#result) 返回 Null

标签 java spring spring-boot http-caching spring-cache

我有一个 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

被抛出。

enter image description here

任何帮助将不胜感激。

最佳答案

不要使用unless="# result!= null",而是使用condition="#result != null"

关于java - Spring Boot 缓存 SpEL (#result) 返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54985969/

相关文章:

java - 为什么我的 ApplicationContext 在我启用网络安全时无法加载?

java - 在 webview 中仅显示网站的一部分

java - 设置新的蒙版抓取 JAVA

java - 将Treecache集群属性存储在属性文件中

java - UnitTesting Spring Boot 中 Unresolved 依赖关系

java - jackson :反序列化为每个值都具有正确类型的 Map<String, Object>

java - 我如何使用hibernate在mysql集群中创建表

java - Webapp 无法在 Tomcat 8.0.23 下运行

java - 如何制作可执行的jar?

java - 如何在spring boot中注册ServletContextListener