➤ 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
}
Result:
{
"name": "John",
"age": 25
}
➤ 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");
}
📦 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>
Under the hood, Spring Boot uses:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
⚙️ 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;
}
Or use annotations:
@JsonProperty("user_name")
private String name;
@JsonIgnore
private String password;
✅ 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
|