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
1 week ago - Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can just run · Links · Maven Plugins · Testing · Android Packages · Language Runtime · JVM Languages · Logging Frameworks · JSON Libraries · Java Specifications · Core Utilities · Mocking · Annotation Libraries · Web Assets · HTTP Clients · Logging Bridges · Dependency Injection ·
🌐
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
🌐
Maven Central
central.sonatype.com › artifact › org.springframework.boot › spring-boot-starter-json › 2.3.3.RELEASE
spring-boot-starter-json - Maven - Sonatype
pkg:maven/org.springframework.boot/spring-boot-starter-json@2.3.3.RELEASE ... <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> <version>2.3.3.RELEASE</version> </dependency>
🌐
HowToDoInJava
howtodoinjava.com › home › spring boot 2 › gson with spring boot: dependency and example
Gson with Spring Boot: Dependency and Example
September 5, 2023 - By default, Spring Boot uses Jackson for JSON serialization and deserialization. Include Gson in the Spring boot application by adding the latest version of com.google.code.gson:gson artifact. dependencies { compile group: 'com.google.code.gson', ...
🌐
GitHub
github.com › MieskeB › json-api-spring-boot
GitHub - MieskeB/json-api-spring-boot: This project converts a normal Java object to json:api standard. More information about json:api can be found here: https://jsonapi.org/. Created and maintained by Mindware Software.
<dependency> <groupId>nl.michelbijnen.jsonapi</groupId> <artifactId>json-api</artifactId> <version>1.5.6</version> </dependency> ... We are aware this library has conflicts with spring-boot-starter-test. This is due to a dependency in that library called android-json.
Starred by 10 users
Forked by 3 users
Languages   Java 100.0% | Java 100.0%
Find elsewhere
🌐
Andbin
andbin.dev › home › spring boot: integration of json binding libraries
Spring Boot: integration of JSON binding libraries – andbin.dev
March 6, 2023 - First of all, JSON-B is just only a specification for which there can be various implementations. In Spring Boot 2.x and 3.x, there is no specific “starter” dependency for JSON-B. Therefore, you must declare some dependencies to have the JSON-B API and implementation on the classpath so ...
🌐
GeeksforGeeks
geeksforgeeks.org › spring-boot-consuming-and-producing-json
Spring Boot - Consuming and Producing JSON - GeeksforGeeks
April 9, 2024 - 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.
🌐
Spring
docs.spring.io › spring-boot › docs › 2.3.0.M1 › api › index.html
JSONObject (Spring Boot 2.3.0.M1 API)
JavaScript is disabled on your browser · Frame Alert · This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client. Link to Non-frame version
🌐
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.
🌐
Maven Repository
mvnrepository.com › artifact › org.springframework.boot › spring-boot-starter-json › 2.4.1
Maven Repository: org.springframework.boot » spring-boot-starter-json » 2.4.1
December 11, 2020 - Starter for reading and writing JSON · LicenseApache 2.0 · Tagsjsonspringframeworkstarter · Organization Pivotal Software, Inc. HomePage https://spring.io/projects/spring-boot 🔍 Inspect URL · Links · DateDec 11, 2020 · Filespom (3 KB)jar (4 KB)View All · RepositoriesCentralCloudera PubMulesoftSpring ReleasesTerrestrisWSO2 Public+3 more · Ranking · #906in MvnRepository · Vulnerabilities · Vulnerabilities from dependencies: CVE-2024-38820CVE-2024-38809CVE-2024-22262CVE-2024-22259CVE-2024-22243CVE-2022-42004CVE-2022-42003CVE-2021-46877CVE-2021-22118CVE-2020-36518CVE-2016-1000027View 8 more ...
🌐
Spring Framework Guru
springframework.guru › home › how to process json with jackson
How to Process JSON with Jackson - Spring Framework Guru
October 21, 2024 - The proper way for Jackson dependency declaration is to use the Spring Boot curated dependency and not including the version tag on the main Jackson library.
Top answer
1 of 6
198

Add under

 <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>

The following exclusion:

 <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>com.vaadin.external.google</groupId>
            <artifactId>android-json</artifactId>
        </exclusion>
    </exclusions>

Similarly, for Gradle projects:

testCompile("org.springframework.boot:spring-boot-starter-test") {
    exclude group: "com.vaadin.external.google", module:"android-json"
}
2 of 6
47

Background: org.json works great, but has a license clause that some people don't like ("The Software shall be used for Good, not Evil."). So Vaadin wanted to use the library, but couldn't be sure they wouldn't use it for evil someday. Instead, they re-implemented the interface, published android-json and used it as a drop in replacement for org.json. Others began to use android-json as well so that they too would not be bound by the requirement of not using their software for evil.

This is a fine solution, except that when the two libraries are on the classpath, they collide.

Solution: If you get this error from conflicting transitive dependencies, then your best bet is to exclude either Vaadin's android-json library (brought in by Spring), or exclude the org.json library (brought in by another dependency). Vaadin's version is meant to be an identical implementation, but there are subtle differences.

If you're using org.json in your code and it is conflicting with Spring's Vaadin dependency, then I would recommend trying open-json. It's a port of Vaadin's re-implementation of org.json, but they changed the packages so you won't have any conflicts with org.json:json or com.vaadin.external.google:android-json

https://github.com/openjson/openjson

Add gradle dependency:

    implementation('com.github.openjson:openjson:1.0.12')

Or in Maven:

    <dependency>
        <groupId>com.github.openjson</groupId>
        <artifactId>openjson</artifactId>
        <version>1.0.12</version>
    </dependency>

Then update any imports that were being used by org.json classes.

🌐
Jar-download
jar-download.com › artifact-search › spring-boot-starter-json
Download spring-boot-starter-json JAR file with all dependencies
json org.json json-simple com.googlecode.json-simple spring-core org.springframework json-simple com.github.cliftonlabs spring-web org.springframework spring-webmvc org.springframework spring-boot org.springframework.boot spring-beans org.springframework spring-context org.springframework spring-jdbc org.springframework spring-aop org.springframework spring-boot-autoconfigure org.springframework.boot javax.json org.glassfish spring-security-config org.springframework.security spring-security-web org.springframework.security spring-orm org.springframework spring-aspects org.springframework spring-test org.springframework jersey-media-json-jackson org.glassfish.jersey.media spring-context-support org.springframework
🌐
Maven Central
central.sonatype.com › artifact › org.springframework.boot › spring-boot-starter-json
spring-boot-starter-json - Maven Central - Sonatype
pkg:maven/org.springframework.boot/spring-boot-starter-json@Loading... ... <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> <version>4.1.0-M2</version> </dependency>
🌐
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 - It follows a different annotation model, using @SerializedName instead of Jackson’s @JsonProperty. Gson is often preferred for its simple design, but it lacks some of the advanced customization features that Jackson offers. If an application has the Gson dependency in its build file and Jackson is removed, Spring Boot automatically switches to GsonHttpMessageConverter.