Languages
[Edit]
EN

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

7 points
Created by:
Waleed-Gates
322

Quick solution: 

import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.util.Collections;

RestTemplate restTemplate = new RestTemplate();
restTemplate.setMessageConverters(Collections.singletonList(
        new MappingJackson2HttpMessageConverter()));

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.

// internal implementation of setMessageConverters method in spring

package org.springframework.web.client;

public class RestTemplate extends InterceptingHttpAccessor implements RestOperations {
	
	// ...
	
	/**
	 * Set the message body converters to use.
	 * <p>These converters are used to convert from and to HTTP requests and responses.
	 */
	public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
		validateConverters(messageConverters);
		// Take getMessageConverters() List as-is when passed in here
		if (this.messageConverters != messageConverters) {
			this.messageConverters.clear();
			this.messageConverters.addAll(messageConverters);
		}
	}
	
	// ...
	
}

 

In older version of Spring we need to use:

new MappingJacksonHttpMessageConverter()

 

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

import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.util.Collections;

RestTemplate restTemplate = new RestTemplate(
        Collections.singletonList(new MappingJackson2HttpMessageConverter()));

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.

// internal implementation of RestTemplate constructor

package org.springframework.web.client;

public class RestTemplate extends InterceptingHttpAccessor implements RestOperations {
	
	// ...
	
	/**
	 * Create a new instance of the {@link RestTemplate} using the given list of
	 * {@link HttpMessageConverter} to use.
	 * @param messageConverters the list of {@link HttpMessageConverter} to use
	 * @since 3.2.7
	 */
	public RestTemplate(List<HttpMessageConverter<?>> messageConverters) {
		validateConverters(messageConverters);
		this.messageConverters.addAll(messageConverters);
		this.uriTemplateHandler = initUriTemplateHandler();
	}
	
	// ...
	
}

 

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