How to return JSON objects as Response in Spring Boot

In this article, we’ll show you how to return a JSON object as a response in a Spring Boot application. We’ll start by creating a new Spring Boot project using the Spring Initializer, a web-based tool that generates a skeleton Spring Boot project with the necessary dependencies and configurations.

One common use case for web services is returning JSON data as a response to a client’s request. Spring Boot makes it easy to return JSON data as a response by providing built-in support for the Jackson library, which is a powerful tool for converting Java objects to and from JSON.

Setting up the Spring Boot project

Firstly, bootstrap your project with Spring Boot Initializr: https://start.spring.io/

Then, make sure you include the Web dependency to your project so that we can add a Rest Controller:

How to return JSON objects as Response in Spring Boot

Once you have your Spring Boot project set up, you’ll need to create a new controller class to handle incoming HTTP requests. A controller class is a Java class that is responsible for handling a specific type of request, such as handling a GET request to retrieve data or a POST request to save data.

Here’s an example of a simple controller Class which contains two @GetMapping annotations:

@RestController
@RequestMapping("/api")
public class MyController {

    @GetMapping("/data")
    public ResponseEntity<Object> getData() {
        Map<String, String> data = new HashMap<>();
        data.put("key1", "value1");
        data.put("key2", "value2");
        return new ResponseEntity<>(data, HttpStatus.OK);
    }

    @GetMapping("/persons")
    public List<Person> getPersons() {
        return List.of(new Person("John","Doe"), new Person("Adam","Smith"));
    }

}

If you are using the spring-web-starter dependency in your Spring Boot project, the media type that the controller returns by default is application/json. The spring-web-starter dependency includes the Jackson library, which is used by Spring to automatically convert Java objects to and from JSON.

  • As you can see, the first endpoint /data of the controller MyController returns a ResponseEntity object and it is not necessary to specify the media type, because the default one is application/json.
  • Also, the second endpoint /persons of the controller MyController returns a List of Person objects and it is not necessary to specify the media type either, because it will be serialized to json as well, since Jackson is included in the project.

It should be noted that Spring boot will take care of mapping the java object to json automatically, so you don’t need to worry about manually converting it.

To test that your endpoint is working as expected, you can use any http client library to send a GET request. For example, we will use curl to test the /api/data and you should be able to see the json object with key-value pairs returned.. For example:

curl http://localhost:8080/api/data
{"key1":"value1","key2":"value2"}

Conclusion

In conclusion, returning JSON object as a response in a Spring Boot 2 application is a straightforward process thanks to the built-in support for the Jackson library. With just a few lines of code, you can create powerful, high-performance web services that return JSON data to the client.

Source code: https://github.com/fmarchioni/masterspringboot/tree/master/json/demo