Spring:return @ResponseBody“ResponseEntity <list <jsonobject>&gt;”</list <jsonobject>

时间:2014-10-11 22:55:30

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

在控制器中我创建了json数组。如果我返回List<JSONObject>就可以了:

@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);
}

5 个答案:

答案 0 :(得分:23)

而不是

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

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

答案 1 :(得分:19)

现在我返回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);
}

答案 2 :(得分:6)

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

public ResponseEntity<?>

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

返回时,我不使用任何类型(在这种情况下未使用):

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

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

我不知道为什么其他答案对我不起作用(错误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();
}