【问题标题】:Spring ResponseEntity<JSONObject> HttpMediaTypeNotAcceptableExceptionSpring ResponseEntity<JSONObject> HttpMediaTypeNotAcceptableException
【发布时间】:2018-02-07 03:36:35
【问题描述】:

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

【问题讨论】:

  • 嗨@Evgenii我认为这是一个类似的问题this one

标签: json spring


【解决方案1】:

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

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

在我做的服务中

String json = line;
return json;

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

【讨论】:

    【解决方案2】:

    您可以在一个 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> 
    

    【讨论】:

      猜你喜欢
      • 2022-01-04
      • 2011-11-04
      • 1970-01-01
      • 2019-01-30
      • 2015-11-14
      • 2014-12-06
      • 2020-05-23
      • 1970-01-01
      • 2017-04-15
      相关资源
      最近更新 更多