I would like to retrieve the value of a JSON file from a RestTemplate request. Unfortunately, I do not know how to proceed. I am using the following method:
RestTemplate restTemplate = new RestTemplate();
final String url = "http://localhost:3333/command/core/get-csrf-token";
public String getToken() {
String token = restTemplate.getForObject(url, String.class);
System.out.println(token);
return token;
I get a String as output {"token":"myToken"}
Is it possible to retrieve directly the value of the JSON?
Parse the JSON with a library (e.g. Jackson) and grab the token
field.
String json = restTemplate.getForObject(url, String.class);
ObjectMapper mapper = new ObjectMapper();
return mapper.readTree(json).get("token").asText();