[英]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.