Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this short tutorial, we’re going to take a close look at how to work with JSON parameters in Spring MVC.

First, we’ll start with a little bit of background on JSON parameters. Then, we’ll go down the rabbit hole to see how to send JSON parameters in POST and GET requests.

2. JSON Parameters in Spring MVC

Using JSON to send or receive data is a common practice among web developers. The hierarchical structure of the JSON strings offers a more compact and human-readable way to represent HTTP request parameters.

By default, Spring MVC provides out-of-the-box data binding for simple data types such as String. For that purpose, it uses a list of built-in property editors under-the-hood.

However, in real-world projects, we may want to bind more complex data types. For example, it might be handy to be able to map a JSON parameter into a model object.

3. Send JSON Data in POST

Spring provides a straightforward way to send JSON data via POST requests. The built-in @RequestBody annotation can automatically deserialize the JSON data encapsulated in the request body into a particular model object.

In general, we don’t have to parse the request body ourselves. We can use the Jackson library to do all the heavy lifting for us.

Now, let’s see how to send JSON data via a POST request in Spring MVC.

Firstly, we need to create a model object to represent the passed JSON data. For instance, consider the Product class:

public class Product {

    private int id;
    private String name;
    private double price;

    // default constructor + getters + setters

}

Secondarily, let’s define a Spring handler method that accepts POST requests:

@PostMapping("/create")
@ResponseBody
public Product createProduct(@RequestBody Product product) {
    // custom logic
    return product;
}

As we can see, annotating the product argument with @RequestBody is enough to bind the JSON data sent from the clients.

Now, we can test our POST request using cURL:

curl -i \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-X POST --data \
  '{"id": 1,"name": "Asus Zenbook","price": 800}' "http://localhost:8080/spring-mvc-basics-4/products/create"

4. Send JSON Parameter in GET

Spring MVC offers @RequestParam to extract query parameters from GET requests. However, unlike @RequestBody, the @RequestParam annotation supports only simple data types such as int and String.

So, to send JSON, we’ll need to define our JSON parameter as a simple string.

The big question here is: How do we convert our JSON parameter (which is a String) into an object of the Product class?

The answer is pretty simple! The ObjectMapper class provided by the Jackson library offers a flexible way to convert JSON strings into Java objects.

Now, let’s see how to send a JSON parameter via a GET request in Spring MVC. First, we’ll need to create another handler method in our controller to handle GET requests:

@GetMapping("/get")
@ResponseBody
public Product getProduct(@RequestParam String product) throws JsonMappingException, JsonProcessingException {
    Product prod = objectMapper.readValue(product, Product.class);
    return prod;
}

As shown above, the readValue() method allows deserializing the JSON parameter product directly into an instance of the Product class.

Note that we define our JSON query parameter as a String object. Now, what if we want to pass a Product object like we did when using @RequestBody?

To answer this question, Spring provides a concise and flexible solution through custom property editors.

First, we’ll need to create a custom property editor to encapsulate the logic of converting the JSON parameter given as a String to a Product object:

public class ProductEditor extends PropertyEditorSupport {

    private ObjectMapper objectMapper;

    public ProductEditor(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        if (StringUtils.isEmpty(text)) {
            setValue(null);
        } else {
            Product prod = new Product();
            try {
                prod = objectMapper.readValue(text, Product.class);
            } catch (JsonProcessingException e) {
                throw new IllegalArgumentException(e);
            }
            setValue(prod);
        }
    }

}

Next, let’s bind the JSON parameter to an object of Product class:

@GetMapping("/get2")
@ResponseBody
public Product get2Product(@RequestParam Product product) {
    // custom logic
    return product;
}

Lastly, we need to add the last missing piece of the puzzle. Let’s register ProductEditor in our Spring controller:

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(Product.class, new ProductEditor(objectMapper));
}

Bear in mind that we need to URL-encode the JSON parameter to ensure safe transport.

So, instead of:

GET /spring-mvc-basics-4/products/get2?product={"id": 1,"name": "Asus Zenbook","price": 800}

We need to send:

GET /spring-mvc-basics-4/products/get2?product=%7B%22id%22%3A%201%2C%22name%22%3A%20%22Asus%20Zenbook%22%2C%22price%22%3A%20800%7D

5. Conclusion

To sum it up, we saw how to work with JSON in Spring MVC. Along the way, we showcased how to send JSON parameters in POST and GET requests.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW