I need to get a token from an external API and keep it cached. I need an api key, email and password.
Naturally I thought that creating an object with all of these elements would be the way to go:
public class LoginObj {
private String apiKey;
private String email;
private String password;
//... sets, gets and constructors
but it doesn't seem to cache in a method like this:
@Cacheable(value = "token")
public String getToken(LoginClass login) {
System.out.println("no cache");
return webClient.post()
.uri("my/uri")
.header("Api-Key", login.getApiKey())
.accept(MediaType.APPLICATION_JSON)
.bodyValue(new LoginClass( login.getEmail() , login.getPassword() ))
.retrieve()
.bodyToMono(JsonNode.class)
.block()
.get("token")
.asText();
}
the "no cache" println keeps showing up, which shows that the method still been called.
The only way that it seems to work is like this:
@Cacheable(value = "token")
public String getToken(String apiKey, String email, String senha) {
System.out.println("no cache");
return webClient.post()
.uri("my/uri")
.header("Api-Key", apiKey)
.accept(MediaType.APPLICATION_JSON)
.bodyValue(new LoginObj(email, senha))
.retrieve()
.bodyToMono(JsonNode.class)
.block()
.get("token")
.asText();
}
Seems like whenever there is an object as a parameter it won't work. Can I pass an object as a parameter or is this a limitation on this case?