java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.HashMap
The underlying issue was with Maven Plugin in STS. Somehow it was not reflecting pom.xml
declared dependencies inside project and also there were no errors in pom.xml
. I re-installed the maven plugin and updated the project and it worked without any change in code.
No converter found for return value of type: class java.util.HashMap
Just use the simpler solution of springboot mvc:
the pom:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<start-class>Application</start-class>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
The controller:
@RestController
public class MyController {
@RequestMapping(value="/getMapResult")
public @ResponseBody
Map<String,List<?>> getMapResult() {
Map<String,List<?>> result = new HashMap<>();
result.put("a", Arrays.asList("a1","a2"));
result.put("b",Arrays.asList(1,2,3));
return result;
}
}
the test:
curl http://localhost:8080/getMapResult
{"a":["a1","a2"],"b":[1,2,3]}
No converter found for return value of type: class org.json.JSONArray with JAVA Spring boot
Example code:
package com.example.springjsonarray;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
Logger logger = LoggerFactory.getLogger(GreetingController.class);
@GetMapping("/greeting")
public ResponseEntity<?> greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
JSONArray ja = new JSONArray();
JSONObject jo1 = new JSONObject();
jo1.put("username", "Jhon");
jo1.put("photoPath", "Doe");
ja.put(jo1);
JSONObject jo2 = new JSONObject();
jo2.put("username", "Carol");
jo2.put("photoPath", "Doe");
ja.put(jo2);
return ResponseEntity.ok().body(ja.toString());
}
}
And response is:
[{"photoPath":"Doe","username":"Jhon"},{"photoPath":"Doe","username":"Carol"}]
Related Topics
Java 8 Streams: Multiple Filters Vs. Complex Condition
Bootstrap.Yml Not Loading in Spring Boot 2
Chrome Browser Is Not Opening in Selenium Webdriver
How to Handle Incorrect User Input into a Scanner
Selenium Webdriver: Modifying Navigator.Webdriver Flag to Prevent Selenium Detection
How to Run Java Program in Terminal With External Library Jar
How to Store User Input into an Existing Array
In Java: How to Zip File from Byte[] Array
Eclipse: the Resource Is Not on the Build Path of a Java Project
Forwarding the Request from One Jsp to Another Jsp With All Request Parameters
Regular Expression to Remove Everything But Characters and Numbers
Using Streams to Convert a List of Objects into a String Obtained from the Tostring Method
How to Exclude Property from Lombok Builder
Calculating the Sum of Number and Its Reverse in Java
Alternative to Switch Case in Java