Spring Boot Cache SpEL (#result) Returns Null

byteHunt3r :

I have a Spring Boot RESTful API to receive and send SMS to clients. My application connects to our local SMS server and receives and pushes SMS to clients via mobile operators. My application works well. But I wanted to optimize my application by implementing cache. I am using the Simple cache of Spring Boot. I face some challenges when creating new SMS.

All SMSes sent/received are in the form of conversations (per ticket) and have clients attached to it. So I faced difficult saving a client into the cache. Below is createClient() snippet:

@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);
}

When I tried creating a new client, a

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

is thrown.

enter image description here

Any assistance shall be greatly appreciated.

Felipe Flores :

instead of using unless="# result! = null" use condition="#result != null"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=151247&siteId=1