gpt4 book ai didi

json - Spring ResponseEntity HttpMediaTypeNotAcceptableException

转载 作者:行者123 更新时间:2023-12-04 19:42:16 33 4
gpt4 key购买 nike

我尝试在发送 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]}]}

我使用 postman 来检查我的方法。这是我得到的错误
{
"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?

最佳答案

事实证明我实际上并没有需要 JSONObject 获取 json。我可以返回一个字符串,它将被解析为 json。在 Controller 中

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

在我做的服务中
String json = line;
return json;

我仍然不确定为什么返回 JSONObject 是不行的。

关于json - Spring ResponseEntity<JSONObject> HttpMediaTypeNotAcceptableException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45943174/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com