我发送GET请求时尝试返回JSONObject。
方法
@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 :(得分:1)
原来我实际上需要 JSONObject来获取json。我可以返回一个String,它将被解析为json。在控制器中
public ResponseEntity<String> getReport(@PathVariables) throws JSONException {
return new ResponseEntity<>(reportService.getReportJSON(), HttpStatus.OK);
}
在我做的服务中
String json = line;
return json;
我仍然不确定为什么返回JSONObject是不行的。
答案 1 :(得分:0)
您可以在一个POJO类中设置对象并在RestController类中返回,而不是在RestController类中返回直接JSONObject
。
注释级别中的MediaType.APPLICATION_JSON_VALUE
将负责转换。在将来,如果您要同时发送xml
和json
,只需在注释级别扩展更多MediaTypes
,它可以支持Multi MediaTypes
。只需关注this示例。
另外,请确保HttpMessageConversions
包含以下依赖项:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.10</version>
</dependency>