Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring ResponseEntity<JSONObject> HttpMediaTypeNotAcceptableException

Tags:

json

spring

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?

like image 463
Evgenii Avatar asked Nov 08 '22 17:11

Evgenii


1 Answers

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.

like image 67
Evgenii Avatar answered Nov 15 '22 05:11

Evgenii