Java 春天:返回@ResponseBody "ResponseEntity<List<JSONObject>>"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26320106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:13:31  来源:igfitidea点击:

Spring: return @ResponseBody "ResponseEntity<List<JSONObject>>"

javajsonspringresthttp-status-codes

提问by martin

In controller I create json array. If I return List<JSONObject>it is ok:

在控制器中,我创建了 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;
}

but I need to return JSON array and HTTP status code:

但我需要返回 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 see error in XXX line:

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

How can I return json+http reply? There is my working code for returning one json object + http status code:

如何返回 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);
}

采纳答案by martin

Now I return Object. I don't know better solution, but it works.

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

回答by user2428804

Instead of

代替

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

try

尝试

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

回答by Sonata

Personally, I prefer changing the method signature to:

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

public ResponseEntity<?>

This gives the advantage of possibly returning an error message as single item for services which, when ok, return a list of items.

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

When returning I don't use any type (which is unused in this case anyway):

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

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

回答by Sonu patel

I am late for this but i want put some more solution relevant to this.

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

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

回答by Rivenfall

I have no idea why the other answers didn't work for me (error 500) but this works

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