Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

How to return a JSON with two objects?

I am new to the construction of REST services. I want to build a service that when consulting an invoice number shows me the corresponding client.

For example:

enter code here  {
"numIdFactura":"90999909",
"strDescripcion":"Books",
"numIVA":30,
"dtmFecha":"09/12/2018",
"num_cliente": {
  "numIdUsuario":90912,
  "name":"Henry",
  "lastName":"Angel"
}}

This is my source code:

Factura.java (invoice)

@Entity
@Table(name = "tbl_factura")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = { "createdAt", "updatedAt" }, allowGetters = true)
public class Factura {

@Id
@Column(name = "NUM_ID_FACTURA", unique = true, nullable = false)
private int numIdFactura;

@Column(name = "STR_DESCRIPCION")
private String strDescripcion;

@Column(name = "NUM_IVA")
private float numIVA;

@Column(name = "DTM_FECHA")
private Date dtmFecha;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="NUM_IDENTIFIC")
@JsonBackReference
private Cliente cliente; 

Ciente.java (Client)

@Entity
@Table(name = "tbl_cliente")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = { "createdAt", "updatedAt" }, allowGetters = true)

public class Cliente implements Serializable {


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID_USUARIO", unique = true, nullable = false)
    private Integer num_id_usuario;


    @OneToMany(fetch=FetchType.EAGER,mappedBy = "cliente")
    @JsonManagedReference
    private  List<Factura> facturas;

    @Column(name = "STR_TIPO_IDENTIF")
    private String str_tipo_identif;

    @Column(name = "STR_APELLIDOS")
    private String str_apellido;

    @Column(name = "STR_NOMBRES")
    private String str_nombres;

    @Column(name = "STR_TELEFONO")
    private String str_telefono;

    @Column(name = "STR_USUARIO")
    private String str_usuario;

FacturaController.java (invoiceController)

@RestController
@RequestMapping("/api")
public class FacturaController {

    @Autowired
    FacturaRepository facturaRepository;

        @GetMapping("/factura/{num_id_factura}")
        public Optional<Factura> getClienteById(@PathVariable(value="num_id_factura") int factura) {

            Optional<Factura> fac = null;

            fac = facturaRepository.findById(factura);

            return fac;

        }
}

After calling the service through the GET method, it generates the following error in the response:

{
   "timestamp": "2018-12-09T16:45:09.492+0000",
   "status": 500,
   "error": "Internal Server Error",
   "message": "Could not write JSON: Unable to find com.facturacion.contabilidad.model.Cliente with id 1777190; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unable to find com.facturacion.contabilidad.model.Cliente with id 1777190 (through reference chain: com.facturacion.contabilidad.model.Factura[\"cliente\"]->com.facturacion.contabilidad.model.Cliente_$$_jvstd53_1[\"facturas\"])",
   "path": "/api/factura/1"
}

According to the above, you could do object mapping to return a JSON that has the information of the two objects. Thank you .

Comments