DEV Community

Cover image for What Does Jackson Do in a Spring Boot Application?
DrSimple
DrSimple

Posted on

What Does Jackson Do in a Spring Boot Application?

➀ Serialization:

Converts Java objects β†’ JSON
Used when sending responses from controllers.

@GetMapping("/user")
public User getUser() {
    return new User("John", 25);  // πŸ‘ˆ Spring uses Jackson to convert this to JSON
}
Enter fullscreen mode Exit fullscreen mode

Result:

{
  "name": "John",
  "age": 25
}
Enter fullscreen mode Exit fullscreen mode
➀ Deserialization:

Converts JSON β†’ Java objects
Used when receiving data in request bodies.

@PostMapping("/user")
public ResponseEntity<?> createUser(@RequestBody User user) {
    // πŸ‘ˆ Jackson maps incoming JSON into a User object
    return ResponseEntity.ok("User created");
}
Enter fullscreen mode Exit fullscreen mode
πŸ“¦ Where Is Jackson in Spring Boot?

You don’t have to manually add Jackson β€” it’s included automatically with:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId> βœ… includes Jackson
</dependency>
Enter fullscreen mode Exit fullscreen mode

Under the hood, Spring Boot uses:

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
</dependency>
Enter fullscreen mode Exit fullscreen mode
βš™οΈ Customizing Jackson (Optional)

You can customize how JSON is handled:

@Bean
public ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return mapper;
}

Enter fullscreen mode Exit fullscreen mode
Or use annotations:
@JsonProperty("user_name")
private String name;

@JsonIgnore
private String password;
Enter fullscreen mode Exit fullscreen mode
βœ… Summary
Feature Handled by Jackson in Spring Boot
Return JSON from controller βœ… Serialize Java β†’ JSON
Accept JSON in request βœ… Deserialize JSON β†’ Java
Customize format βœ… With annotations or config
Built-in with Spring Boot βœ… via spring-boot-starter-web

Top comments (0)