Languages
[Edit]
EN

Spring Boot - use RestTemplate with JSON only (without XML mapper)

7 points
Created by:
WGates
412

Quick solution: 

Above solution tested with Spring Boot 2.3+

This approach will remove all old rest template message converters and set only the one we pass. In our case we care only about Jackson converter (JSON only). In some cases when we add new XML jackson mapper spring will use XML mapper as default and our REST API won't work with JSON anymore. We can specify what we want from controller (like borwser can request for resources in JSON or XML if one endpoint support more then 1 response type).

Internal implementation of setMessageConverters method in spring. We can see that older message converters are cleared and only new message converters are used.

 

In older version of Spring we need to use:

 

Second approach (add new converter, do not clear old one)

Edit

Internal implementation of RestTemplate contructor, when we pass message converters. As we can see old converters won't be removed and new one will be added. First approach is recommended when we want to remove old one and keep only the one we specified.

 

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join