I am sending a HTTP REST GET request to an API with standard Java 17 like this :
HttpResponse<String> response = httpClient.send(request, BodyHandlers.ofString());
and I can see the with response.body() the correct JSON. I want to translate it directly to a Java record like this:
record GetBearerTokenRespone(String access_token, String expires_in, String token_type)
However, I do not see anything build in the standard library to do so and one have to use external libraries, for example like described here https://openjdk.org/groups/net/httpclient/recipes.html with Jackson.
Seems there is no direct way provided by Java HttpClient to do that details for BodyHandlerClass
This could solved your problem using jackson:
HttpResponse<String> response = httpClient.send(request, BodyHandlers.ofString());
final ObjectMapper mapper = new ObjectMapper() .enable(SerializationFeature.INDENT_OUTPUT);
GetBearerTokenRespone record = mapper.readValue(response.body(), GetBearerTokenRespone.class);