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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building a CRUD Application With Spring and SimpleJdbcMapper
  • How to Marry MDC With Spring Integration
  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux

Trending

  • PWA on iOS, Part 1: PWAs on the iPhone Are Older Than They Look
  • Introducing senro: A Pipeline Engine You Program in Go
  • How Kestra Unifies Enterprise Workflow Orchestration and Agentic AI
  • To Switch, or Not to Switch the AI Model

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Dec. 06, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
85.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building a CRUD Application With Spring and SimpleJdbcMapper
  • 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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook