【问题标题】:Spring Boot Cache SpEL (#result) Returns NullSpring Boot Cache SpEL (#result) 返回 Null
【发布时间】:2019-03-04 15:05:56
【问题描述】:

我有一个 Spring Boot RESTful API 来接收和发送 SMS 给客户端。我的应用程序连接到我们的本地 SMS 服务器,并通过移动运营商接收和推送 SMS 给客户。我的应用程序运行良好。但我想通过实现缓存来优化我的应用程序。我正在使用 Spring Boot 的简单缓存。创建新短信时我面临一些挑战。

所有发送/接收的短信都是对话形式(每张票),并附有客户。所以我面临着将客户端保存到缓存中的困难。下面是createClient() sn-p:

@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

被抛出。

任何帮助将不胜感激。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-14
      • 2020-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-12
      • 2017-06-27
      相关资源
      最近更新 更多