Master JSON Handling in Spring Boot: From Auto‑Configuration to @RequestBody
This guide explains how Spring Boot automatically configures JSON libraries like Jackson, shows how to add the necessary starter, demonstrates returning JSON objects with @RestController, and illustrates receiving JSON payloads via @RequestBody with complete MockMvc test examples.
Spring Boot JSON Support Overview
Spring Boot includes built‑in support for three JSON libraries—Gson, Jackson, and JSON‑B—with Jackson being the default recommended implementation. When the corresponding library is present on the classpath, Spring Boot automatically configures it via its starter modules.
Adding the JSON Starter
The spring-boot-starter-json starter is transitively included when you add spring-boot-starter-web. You can also add it explicitly with the following Maven dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>Returning JSON with @RestController
Define a simple POJO and a controller that returns it. Spring Boot automatically converts the object to JSON using Jackson.
public class User {
private String userNo;
private String username;
private int age;
private Date createDate;
// getters and setters omitted
} @RestController
public class JsonController {
@GetMapping("/userInfo")
public User getUserInfo() {
User user = new User();
user.setUserNo("1000");
user.setUsername("Tom");
user.setAge(18);
user.setCreateDate(new Date());
return user;
}
}The @RestController annotation combines @Controller and @ResponseBody, causing the returned object to be written directly to the HTTP response body as JSON.
Unit Test for GET Endpoint
@SpringBootTest
@AutoConfigureMockMvc
class JsonControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
void getUserInfo() throws Exception {
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/userInfo")).andReturn();
String json = result.getResponse().getContentAsString();
System.out.println("Result: " + json);
}
}Running the test prints a JSON string similar to:
{"userNo":"1000","username":"Tom","age":18,"createDate":"2019-12-31T02:38:31.679+0000"}Receiving JSON with @RequestBody
When a client sends a JSON payload in the request body, use @RequestBody on the method parameter to have Spring convert the JSON to a Java object.
@RestController
public class JsonController {
@PostMapping("/addUser")
public void addUser(@RequestBody User user) {
System.out.println("userNo: " + user.getUserNo());
System.out.println("username: " + user.getUsername());
System.out.println("age: " + user.getAge());
System.out.println("createDate: " + user.getCreateDate());
}
}Unit Test for POST Endpoint
@SpringBootTest
@AutoConfigureMockMvc
class JsonControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
void addUser() throws Exception {
String userJson = "{\"userNo\":\"1000\",\"username\":\"Tom\",\"age\":18,\"createDate\":\"2019-12-31T02:51:52.326+0000\"}";
mockMvc.perform(MockMvcRequestBuilders.post("/addUser")
.content(userJson)
.contentType(MediaType.APPLICATION_JSON));
}
}The test sends the JSON string as the request body and sets the Content-Type header to application/json, allowing Spring’s HttpMessageConverter to deserialize it.
Conclusion
Spring Boot’s auto‑configuration makes JSON handling straightforward: include the appropriate starter, return objects from @RestController methods for automatic serialization, and accept JSON payloads with @RequestBody. The provided MockMvc tests demonstrate how to verify both directions.
Source code repository: https://github.com/secbr/springboot-all/tree/master/springboot-json
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
