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

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

  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  • Ingesting Fixed-Width Mainframe Files Into Delta Lake: The Details Nobody Writes Down
  • From Data Movement to Local Intelligence: The Shift from Centralized to Federated AI
  • Ujorm3: A New Lightweight ORM for JavaBeans and Records
  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
84.6K 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. 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

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

  • 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