Spring Boot 接收带有null字段的JSON

23c0lvtd  于 2023-11-17  发布在  Spring
关注(0)|答案(1)|浏览(155)

我正在开发一个API(只是为了学习),当我尝试使用端点列出保存在我的DB(本地)上的文件时,我收到了这个:

  1. [
  2. {
  3. "nome": null,
  4. "url": null,
  5. "type": null,
  6. "size": 0,
  7. "paciente": null,
  8. "medico": null
  9. }

字符串
]
这是我的终点:

  1. @GetMapping("/files")
  2. public ResponseEntity<List<ResponseFile>> getListFiles() {
  3. List<ResponseFile> files = resultadosExamesService.listarTodosArquivos().map(dbFile -> {
  4. String fileDownloadUri = ServletUriComponentsBuilder
  5. .fromCurrentContextPath()
  6. .path("/exames/files/")
  7. .path(dbFile.getIdExame().toString())
  8. .toUriString();
  9. return new ResponseFile(
  10. dbFile.getNomeArquivo(),
  11. fileDownloadUri,
  12. dbFile.getTipoExame(),
  13. dbFile.getArquivoExame().length);
  14. }).collect(Collectors.toList());
  15. return ResponseEntity.status(HttpStatus.OK).body(files);
  16. }


我的ResponseFile:

  1. public class ResponseFile {
  2. private String nome;
  3. private String url;
  4. private String type;
  5. private long size;
  6. private Paciente paciente;
  7. private PacientesDTO pacienteDTO;
  8. private Medico medico;
  9. public ResponseFile(String nomeArquivo, String fileDownloadUri, String tipoExame, int length) {
  10. this.nome = nome;
  11. this.url = url;
  12. this.type = type;
  13. this.size = size;
  14. }
  15. //get and set...


我期待收到这样的JSON:(这个端点列出了来自DB的医生预约)

  1. [
  2. {
  3. "idConsulta": 2,
  4. "nomeMedico": "Camila Bernardo",
  5. "nomePaciente": "João",
  6. "motivoConsulta": "fortes dores de cabeça",
  7. "dataConsulta": "2023-12-12T14:00:00"
  8. },
  9. {
  10. "idConsulta": 3,
  11. "nomeMedico": "Camila Bernardo",
  12. "nomePaciente": "João",
  13. "motivoConsulta": "fortes dores de cabeça",
  14. "dataConsulta": "2023-12-12T16:00:00"
  15. }


]

kxkpmulp

kxkpmulp1#

如果我没有错的话,你的问题是由于ResponseFile而发生的。我认为ResponseFile不应该是这样的,它应该更像这样。

  1. @Entity
  2. @Table(name="cashes")
  3. public class Cash {
  4. @Id
  5. @Column(name="id")
  6. @GeneratedValue(strategy=GenerationType.IDENTITY)
  7. int id;
  8. @Column(name="storecode")
  9. int storeCode;
  10. @Column(name="storecashcode")
  11. int storeCashCode;
  12. @Column(name="generatedtoken")
  13. String generatedToken;
  14. @Column(name="usage")
  15. int usage;
  16. public Cash(int id, int storeCode, int storeCashCode, String generatedToken,
  17. int usage) {
  18. this.id = id;
  19. this.storeCode = storeCode;
  20. this.storeCashCode = storeCashCode;
  21. this.generatedToken = generatedToken;
  22. this.usage = usage;
  23. }
  24. public Cash() {}
  25. //getters and setters

字符串

展开查看全部

相关问题