2017-08-29 128 views
3

我嘗試在發送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?

+0

嗨@Evgenii我認爲這是類似的問題,像[這一個](https://stackoverflow.com/questions/7197268/spring-mvc-httpmediatypenotacceptableexception) – juanlumn

回答

0

原來我實際上並不需要的JSONObject得到一個JSON。我可以返回一個字符串,它將被解析爲json。在控制器

public ResponseEntity<String> getReport(@PathVariables) throws JSONException { 
    return new ResponseEntity<>(reportService.getReportJSON(), HttpStatus.OK); 
    } 

而且在服務,我做

String json = line; 
return json; 

我仍然不知道爲什麼返回一個JSONObject是一個沒有去,雖然。

0

而不是在RestController類中返回直接JSONObject,您可以設置一個POJO類中的對象,並在RestController類中返回。 MediaType.APPLICATION_JSON_VALUE將在註釋級別處理轉換。未來如果您想同時發送xmljson,則只需在註釋級別擴展更多MediaTypes,以便它可以支持Multi MediaTypes。只要按照this的例子。

另外,還要確保你的包括下面的依賴於HttpMessageConversions

<dependency> 
    <groupId>org.codehaus.jackson</groupId> 
    <artifactId>jackson-mapper-asl</artifactId> 
    <version>1.9.10</version> 
</dependency> 
相關問題