Customizing Spring Boot's ObjectMapper for Date Formatting and JSON Conversion

This tutorial explains how Spring Boot automatically configures Jackson's ObjectMapper, shows how to override it with a custom mapper that formats Date fields, and demonstrates both serialization and deserialization of JSON using the customized ObjectMapper in a Spring MVC controller.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
Customizing Spring Boot's ObjectMapper for Date Formatting and JSON Conversion

Spring Boot initializes ObjectMapper

Spring Boot’s auto‑configuration class JacksonAutoConfiguration contains a nested static configuration that creates a bean named jacksonObjectMapper when Jackson2ObjectMapperBuilder is on the classpath and no other ObjectMapper bean is defined.

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(Jackson2ObjectMapperBuilder.class)
static class JacksonObjectMapperConfiguration {
    @Bean
    @Primary
    @ConditionalOnMissingBean
    ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        return builder.createXmlMapper(false).build();
    }
}

If you provide your own ObjectMapper bean, this auto‑configuration is skipped.

Custom date formatting

Define a configuration class that creates an ObjectMapper with a SimpleDateFormat of "yyyy-MM-dd hh:mm:ss". The mapper will format Date fields such as createDate in the User class accordingly.

public class User {
    private String userNo;
    private String username;
    private int age;
    private Date createDate;
    // getters and setters omitted
}
@Configuration
public class JacksonConfig {
    @Bean
    public ObjectMapper jacksonObjectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"));
        return mapper;
    }
}

Running a unit test shows the JSON output:

{"userNo":"1000","username":"Tom","age":18,"createDate":"2019-12-31 05:15:30"}

Using the ObjectMapper directly

Inject the auto‑configured ObjectMapper into a controller and call writeValueAsString to serialize a User instance.

@RestController
public class JsonController {
    @Autowired
    private ObjectMapper objectMapper;

    @GetMapping("writeString")
    public String writeString() throws JsonProcessingException {
        User user = new User();
        user.setUserNo("1000");
        user.setUsername("Tom");
        user.setAge(18);
        user.setCreateDate(new Date());
        return objectMapper.writeValueAsString(user);
    }
}

For deserialization you can bind JSON to a POJO or read it as a JsonNode tree.

@PostMapping("readJson")
public void readJson(String userJson) throws JsonProcessingException {
    User user = objectMapper.readValue(userJson, User.class);
    System.out.println("userNo: " + user.getUserNo());
    // ... other fields

    JsonNode jsonNode = objectMapper.readTree(userJson);
    JsonNode userNo = jsonNode.get("userNo");
    System.out.println("userNo(from JsonNode): " + userNo.textValue());
}

Unit‑test snippets demonstrate both endpoints.

Source code is available at https://github.com/secbr/springboot-all/tree/master/springboot-json.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaJSONSpring BootJacksonDate FormattingObjectMapper
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.