简体   繁体   中英

No Converter for return type JSONObject

I am using spring mvc and here is my service.

 @RequestMapping(value="/data/{id}",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject data(@PathVariable Long id ,@RequestBody Long Intake ) {
        JSONObject obj = new JSONObject();
        obj.put("test", false);
            System.out.equals(obj);
            return obj;
    }

and it is throwing the error as:

  java.lang.IllegalArgumentException: No converter found for return value of type: class org.json.JSONObject

I have checked many links pointing me that may be the getter/setter are missing or the @ResponseBody annotation is missing.so I ran this code without the need of getter/setter but still showing me this error.

i have imported this dependency too.

import org.json.*;

Any thoughts over this issue??

1 - Try add jackson in your project (if not declared yet).

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.0</version>
</dependency>

Or 2 - return String with ObjectMapper serialization.

@RequestMapping(value="/data/{id}",method=RequestMethod.POST)
@ResponseBody
public String data(@PathVariable Long id ,@RequestBody Long Intake ) {
    ObjectMapper obj = new ObjectMapper();

    Map<String, Object> map =  new HashMap<>();
    map.put("test", false);

    return obj.writeValueAsString(map);
}   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM