【问题标题】:Spring: return @ResponseBody "ResponseEntity<List<JSONObject>>"春天:返回@ResponseBody“ResponseEntity<List<JSONObject>>”
【发布时间】:2014-12-06 20:15:48
【问题描述】:

在控制器中,我创建了 json 数组。如果我返回List&lt;JSONObject&gt; 就可以了:

@RequestMapping(value="", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<JSONObject> getAll() {
    List<Entity> entityList = entityManager.findAll();

    List<JSONObject> entities = new ArrayList<JSONObject>();
    for (Entity n : entityList) {
        JSONObject entity = new JSONObject();
        entity.put("id", n.getId());
        entity.put("address", n.getAddress());
        entities.add(entity);
    }
    return entities;
}

但我需要返回 JSON 数组和 HTTP 状态码:

@RequestMapping(value="", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseEntity<List<JSONObject>> getAll() {
    List<Entity> entityList = entityManager.findAll();

    List<JSONObject> entities = new ArrayList<JSONObject>();
    for (Entity n : entityList) {
        JSONObject Entity = new JSONObject();
        entity.put("id", n.getId());
        entity.put("address", n.getAddress());
        entities.add(entity);
    }
    return new ResponseEntity<JSONObject>(entities, HttpStatus.OK); // XXX
}

Eclipse 在 XXX 行看到错误:

Multiple markers at this line
    - The constructor ResponseEntity<JSONObject>(List<JSONObject>, HttpStatus) is undefined
    - Type mismatch: cannot convert from ResponseEntity<JSONObject> to 
     ResponseEntity<List<JSONObject>>
    - Type mismatch: cannot convert from ResponseEntity<JSONObject> to JSONObject

如何返回json+http回复?我有返回一个 json 对象 + http 状态码的工作代码:

@RequestMapping(value="/{address}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseEntity<JSONObject> getEntity(@PathVariable("address") int address) {
    Entity n = entityManager.findByAddress(address);
    JSONObject o = new JSONObject();
    o.put("id", n.getId());
    o.put("address", n.getAddress());
    return new ResponseEntity<JSONObject>(o, HttpStatus.OK);
}

【问题讨论】:

  • 我认为应该是return new ResponseEntity&lt;List&lt;JSONObject&gt;&gt;(entities, HttpStatus.OK); 对吧?无论如何,您下面的解决方案很好。
  • 谢谢,它有效。什么是更好的? ResponseEntity&lt;List&lt;JSONObject&gt;&gt;ResponseEntity&lt;Object&gt; 你有什么意见?
  • 出于可读性考虑,我认为前者更好。

标签: java json spring rest http-status-codes


【解决方案1】:

现在我返回Object。我不知道更好的解决方案,但它确实有效。

@RequestMapping(value="", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseEntity<Object> getAll() {
    List<Entity> entityList = entityManager.findAll();

    List<JSONObject> entities = new ArrayList<JSONObject>();
    for (Entity n : entityList) {
        JSONObject Entity = new JSONObject();
        entity.put("id", n.getId());
        entity.put("address", n.getAddress());
        entities.add(entity);
    }
    return new ResponseEntity<Object>(entities, HttpStatus.OK);
}

【讨论】:

  • 其他解决方案是使用stackoverflow.com/a/17539455/1974494 @ResponseStatus@ExceptionHandler
  • ResponseEntity 工作的原因很明显,因为 Object 是所有东西的父类
  • 我尝试了同样的方法(返回新的 ResponseEntity(entities, HttpStatus.OK);)但是控制器为我返回了空数组。请有任何建议
【解决方案2】:

代替

return new ResponseEntity<JSONObject>(entities, HttpStatus.OK);

试试

return new ResponseEntity<List<JSONObject>>(entities, HttpStatus.OK);

【讨论】:

    【解决方案3】:

    就个人而言,我更喜欢将方法签名更改为:

    public ResponseEntity<?>
    

    这提供了可能将错误消息作为单个项目返回服务的优势,当服务正常时,返回项目列表。

    返回时我不使用任何类型(在这种情况下无论如何都没有使用):

    return new ResponseEntity<>(entities, HttpStatus.OK);
    

    【讨论】:

      【解决方案4】:

      我迟到了,但我想提出更多与此相关的解决方案。

       @GetMapping
      public ResponseEntity<List<JSONObject>> getRole() {
          return ResponseEntity.ok(service.getRole());
      }
      

      【讨论】:

      • 我在原始问题中没有看到对服务或角色的任何引用,此外,如果不遍历 JSONObjects 列表,如何获得每个 JSONObjects 角色?
      【解决方案5】:

      我不知道为什么其他答案对我不起作用(错误 500),但这有效

      @GetMapping("")
      public String getAll() {
          List<Entity> entityList = entityManager.findAll();
          List<JSONObject> entities = new ArrayList<JSONObject>();
          for (Entity n : entityList) {
              JSONObject Entity = new JSONObject();
              entity.put("id", n.getId());
              entity.put("address", n.getAddress());
              entities.add(entity);
          }
          return entities.toString();
      }
      

      【讨论】:

        猜你喜欢
        • 2018-01-11
        • 2013-10-12
        • 2018-11-19
        • 1970-01-01
        • 1970-01-01
        • 2018-02-07
        • 2023-03-18
        • 1970-01-01
        • 2020-05-23
        相关资源
        最近更新 更多