Java.Lang.Illegalargumentexception: No Converter Found for Return Value of Type

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



Leave a reply



Submit