我嘗試在發送GET請求時返回JSONObject。Spring ResponseEntity <JSONObject> HttpMediaTypeNotAcceptableException
的方法
@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);
}
我得到的JSON從一個文件中。這是一個單一的json對象。在一行中。我把它解析成JSONObject像這樣
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;
這就是我的文件的樣子。
{"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]}]}
我用郵遞員檢查我的方法。這是錯誤我得到
{
"timestamp": 1504020107350,
"status": 406,
"error": "Not Acceptable",
"exception": "org.springframework.web.HttpMediaTypeNotAcceptableException",
"message": "Not Acceptable",
"path": "/audpro/report/1/1/1"
}
我試圖手動創建一個JSONObject並把它傳遞,但得到了同樣的錯誤
JSONObject response = new JSONObject();
response.put("id", 555);
response.put("message", "Provision successful!");
return new ResponseEntity<>(response, HttpStatus.OK);
這是我使用的庫。
import org.codehaus.jettison.json.JSONObject;
爲什麼我不能返回一個jsonobject?
嗨@Evgenii我認爲這是類似的問題,像[這一個](https://stackoverflow.com/questions/7197268/spring-mvc-httpmediatypenotacceptableexception) – juanlumn