[英]Returns an object list in a get Spring Boot request
I created a Spring service to return a list of notifications to the Front End.我创建了一个 Spring 服务来向前端返回通知列表。 However, to make handling the return list easier, I needed to change the return object.
但是,为了更轻松地处理返回列表,我需要更改返回对象。 But I don't know how to do this.
但我不知道如何做到这一点。
Currently the return list looks like this:目前返回列表如下所示:
[ { "id": 24, "titulo": "Titulo da Notificacao", "mensagem": "Mensagem da Notificacao", "dataCriacao": "2021-07-23 17:00:25.244-03", "dataModificacao": null, "status": true }, { "id": 25, "titulo": "Titulo da Notificacao 2", "mensagem": "Mensagem da Notificacao 2", "dataCriacao": "2021-07-23 17:00:25.244-03", "dataModificacao": null, "status": true } ]
I need the feedback to be like this:我需要这样的反馈:
{ "notificacoes": [ { "id": 24, "titulo": "Titulo da Notificacao", "mensagem": "Mensagem da Notificacao", "dataCriacao": "2021-07-23 17:00:25.244-03", "dataModificacao": null, "status": true }, { "id": 25, "titulo": "Titulo da Notificacao 2", "mensagem": "Mensagem da Notificacao 2", "dataCriacao": "2021-07-23 17:00:25.244-03", "dataModificacao": null, "status": true } ] }
@RequestMapping(value = "/status/{status}", method = RequestMethod.GET, produces = { "application/json" }) public List<?> findAllByStatus(@PathVariable("status") Boolean status) { return service.findAllByStatus(status); }
Can anyone help me?谁能帮我? I thought about creating a Notifications object that contains a Notification List.
我想过创建一个包含通知列表的 Notifications 对象。 But I think there is a simpler way to do this...
但我认为有一种更简单的方法可以做到这一点......
Olá, Romeu, o problema é que você está devolvendo uma lista, tens que criar uma classe em que dentro dela exista uma lista. Olá, Romeu, o problema é que você está devolvendo uma lista, tens que criar uma classe em que dentro delaexista uma lista。
Algo desse tipo:算法设计:
public class Notificacoes {
private List<Notificacao> notificacoes = new ArrayList<>();
// Construtores, gets e sets
}
E em seguida chamar no seu controller: E em seguida chamar no seu 控制器:
@RequestMapping(value = "/status/{status}", method = RequestMethod.GET, produces = { "application/json" })
public ResponseEntity<Notificacoes> findAllByStatus(@PathVariable("status") Boolean status) {
return ResponseEntity.ok(new Notificacoes().getNotificacoes().addAll(service.findAllByStatus(status)));
}
Certamente esta não é a melhor forma de fazer, o ideal seria separar as camadas, mas isto já resolve o seu problema. Certamente esta não é a melhor forma de fazer, o Ideal seria separar as camadas, mas isto já resolve o seu problema。
How about using a map ?使用地图怎么样?
This would allow you to set the key "notificacoes" and reuse the method you currently have, providing a list of notifications.这将允许您设置密钥“notificacoes”并重用您当前拥有的方法,提供通知列表。
@RequestMapping(value = "/status/{status}", method = RequestMethod.GET, produces = { "application/json" })
public Map<String, List<Notification>> findAllByStatus(@PathVariable("status") Boolean status) {
Map<String, List<Notification>> response = new HashMap<>();
response.put("notificacoes", service.getNotifications());
return response;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.