β€ 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
|
Top comments (0)