Estoy intentando devolver un DTO desde el controller: Esta es mi Entidad Customer:
@Entity @Table(name="customer") public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "first_name", nullable = false)
private String first_name;
@Column(name="last_name", nullable = false)
private String last_name;
@Column(name="address", nullable = false)
private String address;
@Column(name="city", nullable = false)
private String city;
@OneToMany(mappedBy = "customer", fetch = FetchType.EAGER)
private List<Shipping> shippings = new ArrayList<>();
public Customer() {
}
public Customer(String first_name, String last_name, String address, String city, List<Shipping> shippings) {
this.first_name = first_name;
this.last_name = last_name;
this.address = address;
this.city = city;
this.shippings = shippings;
} //... getter y setters }
Esta es mi Entidad Shipping:
@Entity
@Table(name="shipping")
public class Shipping {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "customer_id")
private Customer customer;
@Column(name="state", nullable = false)
@Enumerated(EnumType.STRING)
private ShippingState shippingState;
@Column(name = "send_date", nullable = false)
private Date send_date;
@Column(name = "arrive_date")
private Date arrive_date;
@Column(name = "priority")
private int priority;
public Shipping() {
}
public Shipping(Customer customer, ShippingState shippingState, Date send_date, Date arrive_date, int priority) {
this.customer = customer;
this.shippingState = shippingState;
this.send_date = send_date;
this.arrive_date = arrive_date;
this.priority = priority;
}
//... getter y setters
}
Este es el CustomerDTO:
public class CustomerDTO {
private Long id;
private String first_name;
private String last_name;
private String address;
private String city;
private List<ShippingDTO> shippings;
public CustomerDTO(Customer customer) {
this.id = customer.getId();
this.first_name = customer.getFirst_name();
this.last_name = customer.getLast_name();
this.address = customer.getAddress();
this.city = customer.getCity();
this.shippings = customer.getShippings().stream().map(ShippingDTO::new).collect(Collectors.toList());
}
//... getter y setters
}
Este es el ShippingDTO:
public class ShippingDTO {
private Long id;
private Customer customer;
private ShippingState shippingState;
private Date send_date;
private Date arrive_date;
private int priority;
public ShippingDTO(Shipping shipping) {
this.id = shipping.getId();
this.customer = shipping.getCustomer();
this.shippingState = shipping.getShippingState();
this.send_date = shipping.getSend_date();
this.arrive_date = shipping.getArrive_date();
this.priority = shipping.getPriority();
}
//...getter y setters
}
Y este es el controller:
@RestController
@RequestMapping("/customers")
public class CustomerController {
@Autowired
private CustomerRepository customerRepository;
@GetMapping("/{id}")
public CustomerDTO getCustomerById(@PathVariable(value = "id") Long customerId) {
Customer customer = customerRepository.findById(customerId).orElse(null);
assert customer != null;
return new CustomerDTO(customer);
}
}
Al consumir la API desde Postman obtengo un mensaje error Aborted desde la aplicación recibo el siguiente mensaje de error: Failure while trying to resolve exception [org.springframework.http.converter.HttpMessageNotWritableException]
Si quito la relación del Customer con Shipping, puedo recibir en postman el customer sin problemas, pero evidentemente tengo un error cuando intenta mostrar los "Shippings" dentro de Customer