DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • RestTemplate vs. WebClient
  • How to Marry MDC With Spring Integration
  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux

Trending

  • How to Submit a Post to DZone
  • DZone's Article Submission Guidelines
  • Testing Java Applications With WireMock and Spring Boot
  • 12 Principles for Better Software Engineering
  1. DZone
  2. Coding
  3. Frameworks
  4. Configuring a Custom ObjectMapper for Spring RestTemplate

Configuring a Custom ObjectMapper for Spring RestTemplate

Learn more about creating a custom ObjectMapper for Spring RestTemplate.

By 
Brian Hannaway user avatar
Brian Hannaway
·
Dec. 06, 19 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
82.7K Views

Join the DZone community and get the full member experience.

Join For Free

old map

Learn more about creating a custom ObjectMapper for Spring RestTemplate.

One of the great things about RestTemplate is its simplicity. You simply instantiate it like this... RestTemplate restTemplate = new RestTemplate(); and off you go. Under the hood, Spring automatically creates and registers a number of message converters to handle various data formats for requests and responses.

A MappingJackson2HttpMessageConverter uses Jackson to map POJOs to JSON and vice versa. When the MappingJackson2HttpMessageConverter is created it's given a new instance of ObjectMapper. A default instance of ObjectMapper is fine is many cases, but there are times when you might want to customize the ObjectMapper used.

You may also like: How to Use Spring RESTTemplate to Post Data to a Web Service

For example, I ran into an issue recently where I needed to configure Jackson to accept case insensitive properties like this.

private ObjectMapper createObjectMapper() {

     ObjectMapper objectMapper = new ObjectMapper();
     objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

     return objectMapper;
}


To make the custom ObjectMapper available to MappingJackson2HttpMessageConverter, simply create a new MappingJackson2HttpMessageConverter and pass in the ObjectMapper instance.

private MappingJackson2HttpMessageConverter createMappingJacksonHttpMessageConverter() {

    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setObjectMapper(createObjectMapper());
    return converter;
}


Then, you can create a RestTemplate and add your custom MappingJackson2HttpMessageConverter to its list of message converters.

@Bean
public RestTemplate createRestTemplate() {

     RestTemplate restTemplate = new RestTemplate();
     restTemplate.getMessageConverters().add(0, createMappingJacksonHttpMessageConverter());
}


Note that you should add it to the beginning of the list so that it takes precedence over the default MappingJackson2HttpMessageConverter that Spring has already registered.

Further Reading

How to Use Spring RESTTemplate to Post Data to a Web Service

Processing JSON With Jackson

Spring Framework resttemplate

Published at DZone with permission of Brian Hannaway, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • RestTemplate vs. WebClient
  • How to Marry MDC With Spring Integration
  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: