As you are using Spring Boot web, Jackson dependency is implicit and we do not have to define explicitly. You can check for Jackson dependency in your pom.xml in the dependency hierarchy tab if using eclipse.

And as you have annotated with @RestController there is no need to do explicit json conversion. Just return a POJO and jackson serializer will take care of converting to json. It is equivalent to using @ResponseBody when used with @Controller. Rather than placing @ResponseBody on every controller method we place @RestController instead of vanilla @Controller and @ResponseBody by default is applied on all resources in that controller.
Refer this link: https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-responsebody

The problem you are facing is because the returned object(JSONObject) does not have getter for certain properties. And your intention is not to serialize this JSONObject but instead to serialize a POJO. So just return the POJO.
Refer this link: https://stackoverflow.com/a/35822500/5039001

If you want to return a json serialized string then just return the string. Spring will use StringHttpMessageConverter instead of JSON converter in this case.

Answer from prem kumar on Stack Overflow
🌐
Spring
docs.spring.io › spring-boot › reference › features › json.html
JSON :: Spring Boot
Spring Boot also provides ObjectValueSerializer and ObjectValueDeserializer base classes that provide useful alternatives to the standard Jackson versions when serializing objects. See ObjectValueSerializer and ObjectValueDeserializer in the API documentation for details. The example above can be rewritten to use ObjectValueSerializer and ObjectValueDeserializer as follows: ... import tools.jackson.core.JsonGenerator; import tools.jackson.core.JsonParser; import tools.jackson.databind.DeserializationContext; import tools.jackson.databind.JsonNode; import tools.jackson.databind.SerializationCon
🌐
Baeldung
baeldung.com › home › spring › spring boot › spring boot consuming and producing json
Spring Boot Consuming and Producing JSON | Baeldung
1 month ago - Learn how to consume and produce JSON content in a typical CRUD REST service developed with a Spring Boot.
🌐
Medium
medium.com › @AlexanderObregon › mechanics-of-json-serialization-and-deserialization-in-spring-boot-4bb02f19dd3e
Mechanics of JSON Serialization and Deserialization in Spring Boot
February 19, 2025 - Learn how Spring Boot handles JSON serialization and deserialization using Jackson, Gson, and Jsonb, covering message converters, custom mappers, and configuration.
Top answer
1 of 10
181

As you are using Spring Boot web, Jackson dependency is implicit and we do not have to define explicitly. You can check for Jackson dependency in your pom.xml in the dependency hierarchy tab if using eclipse.

And as you have annotated with @RestController there is no need to do explicit json conversion. Just return a POJO and jackson serializer will take care of converting to json. It is equivalent to using @ResponseBody when used with @Controller. Rather than placing @ResponseBody on every controller method we place @RestController instead of vanilla @Controller and @ResponseBody by default is applied on all resources in that controller.
Refer this link: https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-responsebody

The problem you are facing is because the returned object(JSONObject) does not have getter for certain properties. And your intention is not to serialize this JSONObject but instead to serialize a POJO. So just return the POJO.
Refer this link: https://stackoverflow.com/a/35822500/5039001

If you want to return a json serialized string then just return the string. Spring will use StringHttpMessageConverter instead of JSON converter in this case.

2 of 10
130

The reason why your current approach doesn't work is because Jackson is used by default to serialize and to deserialize objects. However, it doesn't know how to serialize the JSONObject. If you want to create a dynamic JSON structure, you can use a Map, for example:

@GetMapping
public Map<String, String> sayHello() {
    HashMap<String, String> map = new HashMap<>();
    map.put("key", "value");
    map.put("foo", "bar");
    map.put("aa", "bb");
    return map;
}

This will lead to the following JSON response:

{ "key": "value", "foo": "bar", "aa": "bb" }

This is a bit limited, since it may become a bit more difficult to add child objects. Jackson has its own mechanism though, using ObjectNode and ArrayNode. To use it, you have to autowire ObjectMapper in your service/controller. Then you can use:

@GetMapping
public ObjectNode sayHello() {
    ObjectNode objectNode = mapper.createObjectNode();
    objectNode.put("key", "value");
    objectNode.put("foo", "bar");
    objectNode.put("number", 42);
    return objectNode;
}

This approach allows you to add child objects, arrays, and use all various types.

🌐
ZetCode
zetcode.com › springboot › json
Spring Boot JSON - serving JSON data in a Spring Boot annotation
July 28, 2023 - It allows to read and write data in JSON, Avro, BSON, CBOR, CSV, Smile, (Java) Properties, Protobuf, XML or YAML format. Jackson is auto-configured. It comes with the spring-boot-starter-json. When Jackson is on the classpath an ObjectMapper bean is automatically configured.
🌐
DEV Community
dev.to › scottshipp › parsing-json-in-spring-boot-part-1-513
Parsing JSON in Spring Boot, part 1 - DEV Community
December 8, 2021 - A quick tutorial on different JSON to Java use cases in Spring Boot. Tagged with webdev, java, springboot, jackson.
🌐
Medium
medium.com › @AlexanderObregon › customizing-json-responses-in-spring-boot-apis-a3735056cbd1
Customizing JSON Responses in Spring Boot APIs | Medium
May 16, 2025 - Jackson is what handles the JSON side of things in Spring Boot. Any time your controller returns an object, Jackson takes over and turns that object into a JSON string. It does that by checking your class structure and any annotations you’ve added.
Find elsewhere
🌐
Attacomsian
attacomsian.com › blog › processing-json-spring-boot
How to parse JSON data in Spring Boot
September 28, 2022 - A comprehensive guide to learning how to parse, read, and write JSON in a Spring Boot application.
🌐
CData
cdata.com › kb › tech › json-jdbc-spring-boot.rst
How to connect to JSON Services from Spring Boot
First, we mark the JSON data source as our primary data source. Then, we create a Data Source Bean. Create a DriverManagerDataSource.java file and create a Bean within it, as shown below. If @Bean gives an error, Spring Boot may not have loaded properly. To fix this, go to File -> Invalidate Caches and restart.
🌐
Maven Repository
mvnrepository.com › artifact › org.springframework.boot › spring-boot-starter-json
Maven Repository: org.springframework.boot » spring-boot-starter-json
1 month ago - Home » org.springframework.boot » spring-boot-starter-json · Starter for reading and writing JSON · LicenseApache 2.0 · Tagsjsonspringframeworkstarter · Ranking · #905in MvnRepository · HomePage https://spring.io/projects/spring-boot 🔍 Inspect URL ·
🌐
HowToDoInJava
howtodoinjava.com › home › spring boot › spring boot rest: consuming and producing json
Spring Boot REST: Consuming and Producing JSON
November 25, 2023 - When a Java object needs to be sent as a JSON response, Jackson automatically converts it into a JSON representation. Similarly, when JSON data is received in a request, Spring Boot automatically converts it into the corresponding Java object.
🌐
danvega.dev
danvega.dev › blog › read-json-data-spring-boot-write-database
How to read JSON data in Spring Boot and write to a database
July 5, 2017 - The first problem to solve for was creating some sample JSON, modeling our domain model after it and reading it in. Once you have that you can move on to persisting that data into a database. If you want to check out the source code head over to Github. ... Learn how to build intelligent applications using Retrieval Augmented Generation (RAG) with Spring AI.
🌐
Spring Framework Guru
springframework.guru › home › how to process json with jackson
How to Process JSON with Jackson - Spring Framework Guru
October 21, 2024 - Data binding is a JSON processing model that allows for seamless conversion between JSON data and Java objects. With data binding, you create POJOs following JavaBeans convention with ...
🌐
Medium
farzinpashaeee.medium.com › spring-boot-json-jackson-199804d0143a
Spring Boot JSON — Jackson - Farzin Pashaee - Medium
April 4, 2022 - Jackson is a suite of data-processing tools for Java (and the JVM platform), including the flagship streaming JSON parser/generator library, matching data-binding library (POJOs to and from JSON), and additional data format modules to process data encoded in Avro, BSON, CBOR, CSV, Smile, (Java) Properties, Protobuf, TOML, XML or YAML. In order to use it in the Spring Boot, auto-configuration for Jackson is provided and Jackson is part of spring-boot-starter-json.
🌐
Baeldung
baeldung.com › home › spring › spring boot › load spring boot properties from a json file
Load Spring Boot Properties From a JSON File | Baeldung
July 25, 2025 - Therefore, we have to inject a JSON parser into the loading mechanism. Fortunately, Spring Boot comes with the Jackson library and we can use it through PropertySourceFactory.
🌐
DEV Community
dev.to › ayshriv › spring-boot-rest-api-returning-response-in-json-format-4db0
Spring Boot REST API - Returning Response in JSON Format - DEV Community
February 23, 2025 - Ideal for Web & Mobile Applications: JSON works well with JavaScript and is natively supported in web and mobile development. Spring Boot uses Jackson for JSON serialization and deserialization. Jackson is included by default in Spring Boot ...
🌐
danvega.dev
danvega.dev › blog › 2025 › 11 › 10 › jackson-3-spring-boot-4
Jackson 3 in Spring Boot 4: JsonMapper, JSON Views, and What's Changed
November 10, 2025 - Spring Boot 4 ships with Jackson 3 as its default JSON library, and this brings some changes you'll want to understand before upgrading. If you've been working with Spring for a while, you've probably used Jackson for serializing Java objects ...
🌐
Medium
medium.com › @vino7tech › handling-json-with-objectmapper-in-spring-boot-6fc7ff39088b
Handling JSON with ObjectMapper in Spring Boot | by Vinotech | Medium
October 23, 2024 - import com.fasterxml.jackson.databind.ObjectMapper; public class ObjectToJsonExample { public static void main(String[] args) throws Exception { ObjectMapper objectMapper = new ObjectMapper(); Employee employee = new Employee(1, "John", "Developer"); // Convert Object to JSON String jsonString = objectMapper.writeValueAsString(employee); System.out.println(jsonString); } }