Spring Boot Cache SpEL(#result)返回Null

问题描述 投票:1回答:1

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

当我尝试创建一个新客户端时,a

org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'id' cannot be found on null

被扔了。

enter image description here

任何协助将不胜感激。

java spring spring-boot http-caching spring-cache
1个回答
2
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.