Practical Approaches for JSON Data Reception in Spring Boot REST APIs

Nov 21, 2025 · Programming · 11 views · 7.8

Keywords: Spring Boot | JSON Processing | REST API | HttpMessageNotReadableException | Content-Type Negotiation

Abstract: This article provides an in-depth exploration of various methods for handling JSON data in POST requests within the Spring Boot framework. By analyzing common HttpMessageNotReadableException errors, it details two primary solutions: using Map for structured JSON reception and String for raw JSON string processing. The article includes comprehensive code examples, explains the critical importance of Content-Type configuration, and discusses best practices for JSON parameter passing in API design.

Overview of JSON Processing Mechanism in Spring Boot

When handling JSON data in REST APIs within Spring Boot applications, the framework utilizes the Jackson library by default for serialization and deserialization. When employing the @RequestBody annotation, Spring automatically selects the appropriate message converter based on the method parameter type and the request's Content-Type.

Common Error Analysis

In the original problem, the developer attempted to use @RequestBody String payload to receive a JSON object but encountered an HttpMessageNotReadableException. The root cause of this error is: when the request's Content-Type is application/json, Spring attempts to use Jackson to deserialize the JSON data into the specified Java type. Since the JSON object {"name":"value"} constitutes a complete object structure, while the method parameter is declared as a String type, Jackson cannot directly convert a JSON object into a simple string.

Solution One: Using Map to Receive JSON Objects

For structured JSON data, the most straightforward approach is to use Map<String, Object> as the parameter type:

@RequestMapping(value = "/process", method = RequestMethod.POST)
public void process(@RequestBody Map<String, Object> payload) throws Exception {
    System.out.println(payload);
    // Specific values can be accessed via payload.get("name")
}

This method allows Spring to automatically map the key-value pairs of the JSON object into the Map, preserving the structured nature of the data. The corresponding curl command remains unchanged:

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"name":"value"}' http://localhost:8080/myservice/process

Solution Two: Using String to Receive Raw JSON

If there is a genuine need to process the entire JSON as a string, the controller method can be modified:

@RequestMapping(value = "/process", method = RequestMethod.POST, consumes = "text/plain")
public void process(@RequestBody String payload) throws Exception {
    System.out.println(payload);
    // Manual parsing of the JSON string can be performed here
}

This approach requires explicitly specifying consumes = "text/plain" to instruct Spring to use the string message converter instead of the JSON converter. The corresponding curl command must be adjusted by changing the Content-Type:

curl -H "Accept: application/json" -H "Content-type: text/plain" -X POST -d '{"name":"value"}' http://localhost:8080/myservice/process

Best Practice Recommendations

In practical development, it is recommended to use dedicated DTO (Data Transfer Object) classes for receiving JSON data:

public class RequestDTO {
    private String name;
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
}

@RestController
@RequestMapping("/myservice")
public class BaseService {
    @RequestMapping(value = "/process", method = RequestMethod.POST)
    public void process(@RequestBody RequestDTO request) throws Exception {
        System.out.println(request.getName());
    }
}

This method offers type safety, automatic validation, and improved code maintainability.

Application of JSON Parameters in API Design

The JSON parameter passing method discussed in the reference article shows significant advantages in complex query scenarios. When an API needs to support multiple filter conditions, sorting rules, and pagination parameters, using JSON objects can:

For example, for a search API, the following request structure can be designed:

{
  "filters": [
    {"field": "name", "operator": "contains", "value": "test"},
    {"field": "status", "operator": "equals", "value": "active"}
  ],
  "pagination": {
    "page": 1,
    "size": 20
  },
  "sort": [
    {"field": "createdDate", "direction": "desc"}
  ]
}

Error Handling and Debugging Techniques

When dealing with JSON-related errors, it is advisable to:

Performance Considerations

For high-concurrency scenarios, attention should be paid to:

Conclusion

Spring Boot offers a flexible JSON processing mechanism, allowing developers to choose the appropriate parameter type based on specific requirements. For structured data, using Map or custom DTOs is recommended; for scenarios requiring raw JSON strings, this can be achieved by adjusting the Content-Type. Correct Content-Type configuration and parameter type selection are key to avoiding common JSON deserialization errors.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.