How to Return a JSON Object in a Spring Boot RestController

Question

How can I return a JSON object from my Spring Boot RestController without encountering exceptions?

@RestController
@RequestMapping("/api")
class MyRestController {
    @GetMapping(path = "/hello")
    public ResponseEntity<Map<String, String>> sayHello() {
        Map<String, String> response = new HashMap<>();
        response.put("aa", "bb");
        return ResponseEntity.ok(response);
    }
}

Answer

When trying to return a JSON object using `org.json.JSONObject` in a Spring Boot application, you may encounter an exception due to missing message converters capable of handling that type. This guide will detail the correct approach to return JSON responses and solve the common issue of 'no converter found'.

import org.springframework.http.ResponseEntity;

@RestController
@RequestMapping("/api")
class MyRestController {
    @GetMapping(path = "/hello")
    public ResponseEntity<Map<String, String>> sayHello() {
        Map<String, String> response = new HashMap<>();
        response.put("aa", "bb");
        return ResponseEntity.ok(response);
    }
}

Causes

  • The `org.json.JSONObject` type is not supported by default in Spring Boot because there is no appropriate HttpMessageConverter registered for it.
  • Spring Boot uses Jackson for JSON binding which works with POJOs (Plain Old Java Objects) or collections (Lists, Maps) instead.

Solutions

  • Use a standard Java type like a Map or a custom class to represent your JSON structure.
  • Ensure the ResponseEntity class is used along with appropriate HTTP status codes.

Common Mistakes

Mistake: Returning an object type that isn't recognized by Spring's HttpMessageConverters.

Solution: Instead of returning org.json.JSONObject, return a standard Java type such as Map or a custom POJO.

Mistake: Not using ResponseEntity for structured HTTP responses.

Solution: Wrap your response in ResponseEntity to control the status and response headers.

Helpers

  • Spring Boot return JSON
  • Spring Boot RestController
  • org.json.JSONObject
  • ResponseEntity Spring Boot
  • JSON response in Spring Boot

Related Questions

⦿How Do Synchronized Static Methods Work in Java: Do They Lock on the Class or Object?

Learn how synchronized static methods in Java function. Understand if they lock on the class or the object along with tips on usage and common mistakes.

⦿How to Fix Java SecurityException: Signer Information Does Not Match

Learn how to resolve the Java SecurityException related to signer information mismatch in your classes. Detailed explanation and solutions included.

⦿How to Resolve JAXB IllegalAnnotations Exception Regarding Duplicate Property Names in Java

Learn how to fix JAXB IllegalAnnotationsException related to duplicate property names in your Java classes and ensure proper XML data handling.

⦿What is the Purpose of package-info.java in Java Projects?

Discover the role of packageinfo.java in Java projects its importance and whether you really need it. Learn more about package documentation practices.

⦿How to Retrieve Thread ID from a Thread Pool in Java?

Learn how to retrieve the thread ID from a fixed thread pool in Java when executing tasks. Discover a detailed explanation and sample code.

⦿How to Add a Maven Dependency in Eclipse: A Step-by-Step Guide

Learn how to add Maven dependencies in Eclipse even if youre just starting. Follow our stepbystep guide with expert tips and code snippets.

⦿Why Should You Use '==' Instead of '.equals()' for Null Checks in Java?

Discover why using for null checks in Java is preferred over .equals and learn best practices to avoid NullPointerExceptions.

⦿What are the Advantages of Using Scala/Lift Compared to Java/Spring?

Explore the key benefits of using ScalaLift over JavaSpring for web development including performance coding efficiency and functional programming features.

⦿How to Retrieve Method Parameter Names Using Java Reflection?

Learn how to use Java reflection to obtain method parameter names including examples and common issues.

⦿How to Find the Position of a Substring in a String in Java?

Learn how to find the position of a substring in a string in Java. Discover methods code snippets and common mistakes to avoid.

© Copyright 2025 - CodingTechRoom.com