简体   繁体   English

春天:返回@ResponseBody“ResponseEntity <List<JSONObject> &gt;”

[英]Spring: return @ResponseBody “ResponseEntity<List<JSONObject>>”

In controller I create json array.在控制器中,我创建了 json 数组。 If I return List<JSONObject> it is ok:如果我返回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?如何返回 json+http 回复? There is my working code for returning one json object + http status code:我的工作代码用于返回一个 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);
}

Instead of代替

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

try尝试

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

Now I return Object .现在我返回Object I don't know better solution, but it works.我不知道更好的解决方案,但它有效。

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

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 @ResponseBody,ResponseEntity Spring将Object作为JSON返回 - @ResponseBody , ResponseEntity Spring return Object as JSON Spring MVC @ResponseBody返回一个List - Spring MVC @ResponseBody return a List 如何在Spring MVC中返回包括JSONObject在内的多个ResponseEntity? - How to return multiple ResponseEntity including JSONObject in Spring MVC? Spring使用@ResponseBody返回修改后的JSONObject - Spring returns a modified JSONObject with @ResponseBody Spring - HttpMessageConversionException 使用带有 Map 的 ResponseEntity<string,jsonobject></string,jsonobject> - Spring - HttpMessageConversionException using ResponseEntity with Map<String,JSONobject> 如何对spring mvc控制器发送的ResponseBody或ResponseEntity进行单元测试? - How to unit test a ResponseBody or ResponseEntity sent by a spring mvc Controller? Spring MVC @ResponseBody返回列表不适合使用hibernate的json响应 - Spring MVC @ResponseBody return list is not proper json reponse using hibernate 将@ResponseBody迁移到ResponseEntity - Migrating @ResponseBody to ResponseEntity ResponseEntity 中的 Spring Return 错误格式 - Spring Return error format in ResponseEntity 如何使用ResponseEntity返回JSONObject而不是HashMap? (未找到类型为org.json.JSONObject的返回值的转换器) - How to return a JSONObject instead HashMap with ResponseEntity? (No converter found for return value of type: class org.json.JSONObject)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM