Max
Max

Reputation: 302

Change httpresponse with DTO

I run in container fake smtp and it has own api, but it's not readable and i dont need like 80% of response, so how i can use DTO to make response more readable and less verbose?

 HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("http://localhost:port/api/v2/messages"))
                .method("GET", HttpRequest.BodyPublishers.noBody())
                .build();
        HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());

it responses huge json, i need to implement my DTO to make it more readable and remove unnecessary parts of json

Upvotes: 0

Views: 1085

Answers (1)

anthomaxcool
anthomaxcool

Reputation: 359

I know this is an old question, but...

As found as an answer for this question: Ignore missing properties during Jackson JSON deserialization in Java

You can just add @JsonIgnoreProperties(ignoreUnknown = true) to ignore every field you did not specifically asked for in your DTO.

You can then convert your json to your object jackson like so

ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(response.body(), YourDTO.class);

where response is your HttpResponse.

Upvotes: 1

Related Questions