首页 > 解决方案 > 在 Spring Boot 上保存一对一关系时的无限循环

问题描述

我的控制器上映射了这个非常丑陋的 POST 请求

@PostMapping("/save")
public ResponseEntity<Cliente> save(@RequestBody ClienteSaveBody json) {
    Cliente persona = json.getCliente();
    Ubicacion location = json.getUbicacion();
    String nSector = json.getSector();
    Sector sector = sectorRepo.findById(nSector).orElseThrow(() -> new EntityNotFoundException("sector no encontrado para este id:: " + nSector));
    
    location.setSector(sector);
    location.setCliente(persona);
    persona.setUbicacion(location);
    ubicacionRepo.save(location);
    Cliente obj = clienteRepo.save(persona);
    return new ResponseEntity<Cliente>(obj, HttpStatus.OK);
}

当我使用这个映射请求时,控制台会向我抛出一个无限长的警告。

当我做一个 GET 这是响应:

{
   "nombreCompleto":"John Harold",
   "telefono":"111-111-1111",
   "email":"JH@gmail.com",
   "celular":"222-222-2222",
   "cedula":"22222222222",
   "sexo":"F",
   "fecha_Nacimiento":"2021-04-13T00:00:00.000+00:00",
   "ubicacion":{
      "calle":"Pepin",
      "casa":"2",
      "sector":{
         "nombreSector":"Cambelen",
         "nombreMunicipio":{
            "nombreMunicipio":"Puerto Plata",
            "nombreProvincia":{
               "nombreProvincia":"Puerto Plata"
            }
         }
      },
      "cliente":{
         "nombreCompleto":"John Harold",
         "telefono":"111-111-1111",
         "email":"JH@gmail.com",
         "celular":"222-222-2222",
         "cedula":"22222222222",
         "sexo":"F",
         "fecha_Nacimiento":"2021-04-13T00:00:00.000+00:00",
         "ubicacion":{
            "calle":"Pepin",
            "casa":"2",
            "sector":{
               "nombreSector":"Cambelen",
               "nombreMunicipio":{
                  "nombreMunicipio":"Puerto Plata",
                  "nombreProvincia":{
                     "nombreProvincia":"Puerto Plata"
                  }
               }
            },
            "cliente":{
               "nombreCompleto":"John Harold",
               "telefono":"111-111-1111",
               "email":"JH@gmail.com",
               "celular":"222-222-2222",
               "cedula":"22222222222",
               "sexo":"F",
               "fecha_Nacimiento":"2021-04-13T00:00:00.000+00:00",
               "ubicacion":{
                  "calle":"Pepin",
                  "casa":"2",
                  "sector":{
                     "nombreSector":"Cambelen",
                     "nombreMunicipio":{
                        "nombreMunicipio":"Puerto Plata",
                        "nombreProvincia":{
                           "nombreProvincia":"Puerto Plata"
                        }
                     }
                  },
                  "cliente":{
                     "nombreCompleto":"John Harold",
                     "telefono":"111-111-1111",
                     "email":"JH@gmail.com",
                     "celular":"222-222-2222",
                     "cedula":"22222222222",
                     "sexo":"F",
                     "fecha_Nacimiento":"2021-04-13T00:00:00.000+00:00",
                     "ubicacion":{
                        "calle":"Pepin",
                        "casa":"2",
                        "sector":{
                           "nombreSector":"Cambelen",
                           "nombreMunicipio":{
                              "nombreMunicipio":"Puerto Plata",
                              "nombreProvincia":{
                                 "nombreProvincia":"Puerto Plata"
                              }
                           }
                        },
                        "cliente":{
                           "nombreCompleto":"John Harold",
                           "telefono":"111-111-1111",
                           "email":"JH@gmail.com",
                           "celular":"222-222-2222",
                           "cedula":"22222222222"

标签: springspring-bootspring-mvcspring-data-jpa

解决方案


您可以在模型中使用@JsonIgnore上面的映射。

@JsonIgnore
@OneToMany(mappedBy = "table_name",
            cascade = {CascadeType.REMOVE})

推荐阅读