Reputation: 2913
I am learning to use Spring, using Spring-boot.
I want to return a JSONObject from a Controller but it always return 406.
I imported a org.json.JSONObject; to create a JSONObject.
Code :
@RequestMapping(value = "/json", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Object testJsonCall(){
JSONObject reply = new JSONObject();
reply.put("status","success");
reply.put("foo", "bar");
return reply;
}
Thanks for help.
Upvotes: 0
Views: 908
Reputation: 2913
Thanks everyone. Now I've found a solution.
Initially, I tried adding jackson-core and jackson-core-asl but it didn't make it to work. Wierdly, I've just added toString to the return, it is working !
@RequestMapping(value = "/json", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Object testJsonCall(){
JSONObject reply = new JSONObject();
reply.put("status","success");
reply.put("foo", "bar");
return reply.toString(); //here I added toString()
}
Upvotes: 1
Reputation: 3205
You need to add following jars to pom.xml.
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.4.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.1.1</version> </dependency>
<dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency>
Upvotes: 0