I tried with your code with a sample spring boot project and I get the error,

No converter for [class org.json.JSONObject]

The reason for this error is explained clearly here. To reiterate the answer, JSONObject classes don't have getters and hence the error. By default spring-boot starter web dependency has Jackson web support which can convert any POJO class to JSON object. So as the answer by @süleyman-can using a POJO is the right way to handle this.

In case, you can't use a POJO class because the fields in the response will be different for each request. For example, you have to send

{"a": "b"}

for one response and

{"c": "d"}

for another response, you can always use Map<String, String> like this,

@RequestMapping(value = "/test", method = RequestMethod.GET)
@ResponseBody
public Map<String, String> test() {
  Map<String, String> test = new HashMap<>();
  test.put("name","caroline");
  return test;
}

and the response would come like this,

{"name":"caroline"}                                                                                                                      
Answer from Malathi on Stack Overflow
🌐
Maven Repository
mvnrepository.com › artifact › org.springframework.boot › spring-boot-starter-json
Maven Repository: org.springframework.boot » spring-boot-starter-json
4 days ago - Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can just run ... aar android apache api arm assets build build-system bundle client clojure cloud config cran data database eclipse example extension framework github gradle groovy io ios javascript jvm kotlin library logging maven mobile module npm osgi persistence plugin resources rlang sdk server service spring sql starter ...
🌐
Medium
medium.com › @abdullahkhames96 › understanding-jackson-in-spring-boot-a-comprehensive-guide-c6424d9443f6
jsonUnderstanding Jackson in Spring Boot: A Comprehensive Guide | by Abdullah khames | Medium
October 25, 2024 - Jackson simplifies the conversion ... conversion of JSON to Java objects (deserialization), supporting a wide range of features that cater to different use cases. The Jackson library is modular, and Spring Boot uses it by default in its spring-boot-starter-web and ...
🌐
Spring
docs.spring.io › spring-boot › reference › features › json.html
JSON :: Spring Boot
import tools.jackson.core.JsonGenerator import tools.jackson.core.JsonParser import tools.jackson.databind.DeserializationContext import tools.jackson.databind.JsonNode import tools.jackson.databind.SerializationContext import tools.jackson.databind.ValueDeserializer import tools.jackson.databind.ValueSerializer import org.springframework.boot.jackson.JacksonComponent @JacksonComponent class MyJacksonComponent { class Serializer : ValueSerializer<MyObject>() { override fun serialize(value: MyObject, jgen: JsonGenerator, serializers: SerializationContext) { jgen.writeStartObject() jgen.writeStr
🌐
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.
🌐
Baeldung
baeldung.com › home › spring › spring boot › spring boot consuming and producing json
Spring Boot Consuming and Producing JSON | Baeldung
February 19, 2026 - In this article, we learned how to consume and produce JSON content in a typical CRUD REST service developed with Spring Boot.
🌐
Maven Central Repository
search.maven.org › org.springframework.boot › spring-boot-starter-json › 2.2.3.release
spring-boot-starter-json
April 12, 2024 - <?xml version="1.0" encoding="UTF-8"?> ... <version>2.2.3.RELEASE</version> </parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> <version>2.2.3.RELEASE</version> <name>Spring Boot Json Starter</name> <description>Starter for ...
🌐
Maven Central
central.sonatype.com › artifact › org.springframework.boot › spring-boot-starter-json › 2.3.3.RELEASE
spring-boot-starter-json - Maven - Sonatype
--> <!-- do_not_remove: published-with-gradle-metadata --> <modelVersion>4.0.0</modelVersion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> <version>2.3.3.RELEASE</version> <name>spring-boot-starter-json</name> <description>Starter for reading and writing json</description> <url>https://spring.io/projects/spring-boot</url> <organization> <name>Pivotal Software, Inc.</name> <url>https://spring.io</url> </organization> <licenses> <license> <name>Apache License, Version 2.0</name> <url>https://www.apache.org/licenses/LICENSE-2.0</url> </license> </l
Find elsewhere
🌐
Spring
spring.io › blog › 2025 › 10 › 07 › introducing-jackson-3-support-in-spring
Introducing Jackson 3 support in Spring
If you want to keep using temporarily Jackson 2 support instead of Jackson 3, you can for example: Exclude spring-boot-jackson direct or transitive dependencies (from spring-boot-starter-jackson or spring-boot-starter-json for example) and use spring-boot-jackson2 instead
🌐
GeeksforGeeks
geeksforgeeks.org › spring-boot-consuming-and-producing-json
Spring Boot - Consuming and Producing JSON - GeeksforGeeks
December 3, 2025 - Below are the steps and implementation to demonstrate Spring Boot Consuming and Producing JSON. First create a basic Spring Boot Stater project by using Spring initializr with required project dependencies. ... dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' }
🌐
GitHub
github.com › spring-projects › spring-boot › tree › main › starter › spring-boot-starter-json
spring-boot/starter/spring-boot-starter-json at main · spring-projects/spring-boot
Spring Boot helps you to create Spring-powered, production-grade applications and services with absolute minimum fuss. - spring-boot/starter/spring-boot-starter-json at main · spring-projects/spring-boot
Author   spring-projects
🌐
HowToDoInJava
howtodoinjava.com › home › spring boot › spring boot rest: consuming and producing json
Spring Boot REST: Consuming and Producing JSON
November 25, 2023 - Any Spring @RestController in a Spring Boot application should render JSON response by default as long as Jackson is on the classpath. In the given example, EmployeeList will be serialized by Jackson and serve as a JSON representation to the client.
🌐
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 ... 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-js...
🌐
Maven Repository
mvnrepository.com › artifact › org.springframework.boot › spring-boot-starter-json › 3.5.12
Maven Repository: org.springframework.boot » spring-boot-starter-json » 3.5.12
4 days ago - HomePage https://spring.io/projects/spring-boot 🔍 Inspect URL ... aar android apache api arm assets build build-system bundle client clojure cloud config cran data database eclipse example extension framework github gradle groovy io ios javascript jvm kotlin library logging maven mobile module npm osgi persistence plugin resources rlang sdk server service spring sql starter ...
🌐
DEV Community
dev.to › alexmercedcoder › how-to-build-a-java-spring-json-api-from-a-blank-maven-project-311m
How to build a Java Spring JSON API from a blank maven project - DEV Community
3 weeks ago - Here's an example: <dependencies> <!-- Spring Boot Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Starter Data JPA --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- PostgreSQL Driver --> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.2.14</version> </dependency> </dependencies> Save the pom.xml file and let Maven resolve the dependencies.
🌐
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 - We'll build a donut menu API that demonstrates reading JSON files, configuring the JsonMapper, and using @JsonView to control what data gets sent to different API consumers. Follow along with the complete working example. ... When you add spring-boot-starter-webmvc to a Spring Boot 4 project, you'll get both Jackson 2 and Jackson 3 on your classpath.
🌐
Spring
spring.io › guides › gs › rest-service
Getting Started | Building a RESTful Web Service
Because Jackson is on the classpath, Spring’s JacksonJsonHttpMessageConverter is automatically chosen to convert the Greeting instance to JSON. The Spring Initializr creates an application class for you. In this case, you do not need to further modify the class. The following listing shows the RestServiceApplication application class (in src/main/java/com/example/restservice/RestServiceApplication.java for Java or src/main/kotlin/com/example/restservice/RestServiceApplication.kt for Kotlin): ... package com.example.restservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class RestServiceApplication { public static void main(String[] args) { SpringApplication.run(RestServiceApplication.class, args); } }
🌐
GeeksforGeeks
geeksforgeeks.org › spring-rest-json-response
Spring - REST JSON Response - GeeksforGeeks
March 24, 2025 - It is used because it simplifies the development process by providing default configurations and also reduces boilerplate code. In this article, we will cover the steps to create a simple Spring Boot applicat · 2 min read Spring - init() and destroy() Methods with Example
🌐
CData
cdata.com › kb › tech › json-jdbc-spring-boot.rst
How to connect to JSON Services from Spring Boot
You will also find the sample data used in the following examples. The data includes entries for people, the cars they own, and various maintenance services performed on those cars. After setting the properties in the application.properties file, we now configure them. 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 ...
🌐
Java67
java67.com › 2023 › 04 › how-to-accept-and-produce-json-as.html
How to Accept and Produce JSON as a response in Spring Boot? Example Tutorial | Java67
Consuming JSON data is simple and straightforward using Spring Boot. Now let’s see how to return JSON response using Spring Boot. It is to note here that, Spring Boot by default has Jackson dependency as part of spring-boot-starter-json.