Spring Boot 接收带有null字段的JSON

23c0lvtd  于 7个月前  发布在  Spring
关注(0)|答案(1)|浏览(46)

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

[
{
    "nome": null,
    "url": null,
    "type": null,
    "size": 0,
    "paciente": null,
    "medico": null
}

字符串
]
这是我的终点:

@GetMapping("/files")
    public ResponseEntity<List<ResponseFile>> getListFiles() {
        List<ResponseFile> files = resultadosExamesService.listarTodosArquivos().map(dbFile -> {
            String fileDownloadUri = ServletUriComponentsBuilder
                    .fromCurrentContextPath()
                    .path("/exames/files/")
                    .path(dbFile.getIdExame().toString())
                    .toUriString();

            return new ResponseFile(
                    dbFile.getNomeArquivo(),
                    fileDownloadUri,
                    dbFile.getTipoExame(),
                    dbFile.getArquivoExame().length);
        }).collect(Collectors.toList());

        return ResponseEntity.status(HttpStatus.OK).body(files);
    }


我的ResponseFile:

public class ResponseFile {
    private String nome;
    private String url;
    private String type;
    private long size;
    private Paciente paciente;

    private PacientesDTO pacienteDTO;
    private Medico medico;

    public ResponseFile(String nomeArquivo, String fileDownloadUri, String tipoExame, int length) {
        this.nome = nome;
        this.url = url;
        this.type = type;
        this.size = size;

    }
//get and set...


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

[
{
    "idConsulta": 2,
    "nomeMedico": "Camila Bernardo",
    "nomePaciente": "João",
    "motivoConsulta": "fortes dores de cabeça",
    "dataConsulta": "2023-12-12T14:00:00"
},
{
    "idConsulta": 3,
    "nomeMedico": "Camila Bernardo",
    "nomePaciente": "João",
    "motivoConsulta": "fortes dores de cabeça",
    "dataConsulta": "2023-12-12T16:00:00"
}


]

kxkpmulp

kxkpmulp1#

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

@Entity
@Table(name="cashes")
public class Cash {
  @Id
  @Column(name="id")
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  int id;

  @Column(name="storecode")
  int storeCode;

  @Column(name="storecashcode")
  int storeCashCode;

  @Column(name="generatedtoken")
  String generatedToken;

  @Column(name="usage")
  int usage;

  public Cash(int id, int storeCode, int storeCashCode, String generatedToken,
        int usage) {
      this.id = id;
      this.storeCode = storeCode;
      this.storeCashCode = storeCashCode;
      this.generatedToken = generatedToken;
      this.usage = usage;
  }

  public Cash() {}

  //getters and setters

字符串

相关问题