Reputation: 449
I try to return a JSONObject when a GET request is sent.
The method
@RequestMapping(value = "/{businessId}/{orderId}/{reportId}", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<JSONObject> getReport(@PathVariable("businessId") String businessId,
@PathVariable("orderId") String orderId,
@PathVariable("reportId") Long reportId) throws JSONException {
return new ResponseEntity<JSONObject>(reportService.getReportJSON(), HttpStatus.OK);
}
I get json from a file. The is a single json object there. In one line. I parse it to JSONObject like this
fs = FileSystem.get(uri, conf);
BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader(fs.open(path)));
line = reader.readLine();
while (line != null) {
jsonObjectList = new JSONObject(line);
line = reader.readLine();
}
return jsonObjectList;
This is what my file looks like.
{"reportId":"1","description":"СегментацияпоПоливозраст","orderId":"357","businessId":"НашКлиент№1","tables":[{"name":"Мужчины","fields":[{"name":"0-17","type":"number"},{"name":"18-24","type":"number"},{"name":"25-34","type":"number"},{"name":"35-44","type":"number"},{"name":"45-54","type":"number"},{"name":"45-54","type":"number"},{"name":"45-54","type":"number"}],"data":[2571,5287,4587,7705,3675,3743,7423]},{"name":"Женщины","fields":[{"name":"0-17","type":"number"},{"name":"18-24","type":"number"},{"name":"25-34","type":"number"},{"name":"35-44","type":"number"},{"name":"45-54","type":"number"},{"name":"45-54","type":"number"},{"name":"45-54","type":"number"}],"data":[7552,3107,6477,4967,9106,7859,9060]},{"name":"Мужчиныиженщины","fields":[{"name":"0-17","type":"number"},{"name":"18-24","type":"number"},{"name":"25-34","type":"number"},{"name":"35-44","type":"number"},{"name":"45-54","type":"number"},{"name":"45-54","type":"number"},{"name":"45-54","type":"number"}],"data":[7552,3107,6477,4967,9106,7859,9060]}]}
I use postman to check my methods. This is the error I get
{
"timestamp": 1504020107350,
"status": 406,
"error": "Not Acceptable",
"exception": "org.springframework.web.HttpMediaTypeNotAcceptableException",
"message": "Not Acceptable",
"path": "/audpro/report/1/1/1"
}
I tried to create a jsonobject by hand and pass it, but got the same error
JSONObject response = new JSONObject();
response.put("id", 555);
response.put("message", "Provision successful!");
return new ResponseEntity<>(response, HttpStatus.OK);
This is the library I use.
import org.codehaus.jettison.json.JSONObject;
Why can't I return a jsonobject?
Upvotes: 5
Views: 23354
Reputation: 21
You can better use toMap()
function and then return it.
Sample code
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(jsonObject.toMap());
Upvotes: 0
Reputation: 449
Turns out I don't actually need JSONObject to get a json. I can return a String and it will be parsed as json. In the controller
public ResponseEntity<String> getReport(@PathVariables) throws JSONException {
return new ResponseEntity<>(reportService.getReportJSON(), HttpStatus.OK);
}
And in the service I do
String json = line;
return json;
I'm still not sure about why returning a JSONObject is a no go though.
Upvotes: 1
Reputation: 175
Instead of returning direct JSONObject
in RestController class, you can set the objects in one POJO class and return in RestController class.
MediaType.APPLICATION_JSON_VALUE
in the annotation level will take care of conversion.In Future if you want to send both xml
and json
, simply you can extend more MediaTypes
in annotation level, so that it can support Multi MediaTypes
. Just follow this example.
Also make sure your including below dependency for HttpMessageConversions
:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.10</version>
</dependency>
Upvotes: 0