DEV Community

André Moriya
André Moriya

Posted on

1

ObjectMapper: Conversion to generic types.

Hey folks!

I decided to write this post because I needed to do a conversion using generic classes.

It's quite simple, but I believe that it can be helpful in the future, both for myself and for other devs.

What I was needed to get a JSON callback coming from a rest service, developed in the company.

The code looks like this:

CloseableHttpClient client = HttpClients.createDefault();

// Code omitted...
response = client.execute(request);
// Code omitted...

BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
ObjectMapper mapper = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
ResponseApiDTO<ProductDTO> response = mapper.readValue(br.readLine(), new TypeReference<ResponseApiDTO<ProductDTO>>() {});

ProductDTO dto = response.getDto();
Enter fullscreen mode Exit fullscreen mode

That was basically it.
As the project doesn't use spring, this was the way implemented.

Well, that's it, I hope this post could have helped.

Thanks

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay