Ivan Assalim
Ivan Assalim

Reputation: 59

Spring RESTful Webservice - Returning JSON without model object

there.
I have a doubt with returning JSONObjects on a Spring RESTful WebService.

Here it goes:
I have a method in my controller which I want to have it returning a JSONObject. However, when I set it's return type to JSONObject and effectively return a JSONObject, I get that following error:

HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class org.json.JSONObject

So, I actually understand what that means, and I've been searching a answer to that issue for at least 3 days.

Here is my code:

@RequestMapping(value = "/value", method = RequestMethod.POST)
public String method(HttpServletRequest request) {
    JSONObject json = new JSONObject();
    json.put("example", "example message");

    return json.toString();
}

I trully don't know if that's gonna work when I have to consume it on the front-end (Which is going to be an external application). Do I have to return a true JSONObject? Or returning a JSONObject.toString() should work fine?

And one last thing:
Most tutorials about returning a JSONObject teaches that proccess using a model object, which I don't want to use. Is there a way of doing that without using a model object?

Thanks in advance, peeps!

Upvotes: 2

Views: 5619

Answers (6)

Farshad Bayat
Farshad Bayat

Reputation: 77

It is very easy i solved this way:

@RequestMapping({"/jsontest"})
    public @ResponseBody
    Map<String, Object> jsonTest() {
        String json = "{\"name\":\"farshad\", \"age\":34 , \"list\": [ 1 , 2 , 3 ] , \"objects\": [ {\"name\":\"a\", \"age\":\"10\"} , {\"name\":\"b\", \"age\":\"20\"} , {\"name\":\"c\", \"age\":\"30\"} ] }"; 
        ObjectMapper mapper = new ObjectMapper();

        // convert JSON string to Map
        Map<String, Object> map = mapper.readValue(json, Map.class);
        return map;
    }

Dependency:

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

Imports:

/* Json Imports */
import com.fasterxml.jackson.databind.ObjectMapper;    
import java.util.Map;

Result:

Upvotes: 0

softwarevamp
softwarevamp

Reputation: 857

You can also use anonymous object:

new Object() {
    public final int code = 0;

    public final List<T> data = ...;
}

Upvotes: 0

itachi
itachi

Reputation: 3597

I had the same issue, and the solution is unbelievably simple. I assume that you have Jackson in your dependencies, and then you can do as follows:

After you create a JSONObject you want to return, simply write: return jsonObject.toMap(), and Jackson will do the rest of the work. Don't forget to change the return type of your method to Map<String, Object>, and apply appropriate annotations: @ResponseBody on the method, or @RestController on a whole class. It depends on your needs.

In your case it will be:

@RequestMapping(value = "/value", method = RequestMethod.POST)
public @ResponseBody Map<String, Object> method(HttpServletRequest request) {
    JSONObject json = new JSONObject();
    json.put("example", "example message");

    return json.toMap();
}

Upvotes: 1

Frederic Kneier
Frederic Kneier

Reputation: 23

If you are using Jackson for mapping you can return an ObjectNode instead. It is Jackson's representation of a json object. There is also an ArrayNode class. If you want to use your own implementation then either write an HttpMessageConverter that allows reading and writing json or use the toString method if this actually returns a json string.

Upvotes: 0

no id
no id

Reputation: 1672

Change class annotation to @RestController, then return model object or Map<String, Object>. You don't need to mess with JSONObject for the task.

@ResponseBody for the method, like akhill answered, is also ok but too verbose if all the controller is REST.

Upvotes: 0

Akhil
Akhil

Reputation: 1274

Just annotate your method with @ResponseBody.

Upvotes: 0

Related Questions