Spring Boot JSON to POJO
Stop manually parsing strings. Learn how to leverage Spring's Auto-Magic Configuration, handle Generic Lists, and master WebClient.
The Automagic: @RequestBody
In plain Java (like with Jackson or Gson), you have to manually create an object mapper and call `readValue`.
What is @RequestBody? It is a Spring annotation that instructs the `HttpMessageConverter` to deserialize the raw HTTP Request body into a Java Object (POJO), typically using the Jackson library.
Spring Boot removes this step using HttpMessageConverters. If Jackson is on the classpath (it is by default), Spring automatically detects `application/json` headers and converts the body.
@RestController
@RequestMapping("/api/users")
public class UserController {
@PostMapping
public ResponseEntity<User> createUser(@RequestBody User user) {
// 'user' is already a fully populated POJO!
// No manual parsing needed.
return ResponseEntity.ok(userService.save(user));
}
}Note: The @RequestBody annotation tells Spring to look at the HTTP Body. Without it, your POJO will be null.
Fetching JSON: Legacy vs Modern
RestTemplate
User user = restTemplate.getForObject( "https://api.com/users/1", User.class );
Simple and effective for traditional blocking applications. Deprecated in favor of WebClient for non-blocking needs.
WebClient
Mono<User> user = webClient.get()
.uri("/users/1")
.retrieve()
.bodyToMono(User.class);Non-blocking, functional style. Offers better performance under high load.
Essential Edge Cases
1. The "List<T>" Erasure Problem
You cannot call getForObject(url, List<User>.class) because Java deletes generics at runtime. You will get a LinkedHashMap error.
The Fix: ParameterizedTypeReference
List<User> users = restTemplate.exchange(
"https://api.com/users",
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<User>>() {}
).getBody();2. The Date Format Nightmare
By default, Jackson often serializes dates as timestamps (numbers). To use ISO-8601 strings (e.g., "2023-01-01"), configure your application.properties:
spring.jackson.serialization.write-dates-as-timestamps=false spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
Common Spring Errors
Error: "Can not write JSON: Infinite Recursion (StackOverflowError)"
This often happens with Hibernate Entities (`@OneToMany` / `@ManyToOne`). Jackson tries to serialize the Parent -> Child -> Parent loop forever.
Could not write JSON: Infinite recursion (StackOverflowError); nested exception is...Fix: Use @JsonManagedReference on the parent side and @JsonBackReference on the child side. Or simply using DTOs (Data Transfer Objects) instead of returning Entities directly.
How to map JSON Object to POJO in Spring Boot?
Spring Boot uses `Jackson` by default. Simply define your POJO as a parameter in your Controller and annotate it with `@RequestBody`. Spring handles the rest.
How do I ignore unknown JSON fields?
Set spring.jackson.deserialization.fail-on-unknown-properties=false in your properties file. This prevents 400 Bad Request errors when the API changes.
Need the POJO Class first?
Before mapping in Spring, you need the Java class. Our tool generates Lombok-ready code instantly.