Skip to content
Advertisement

How to deserialize from Number value to int (error – no int/Int-argument constructor/factory method to deserialize from Number value)

I have two classes Flight and Ticket. One flight can has lots of tickets, so Ticket has foreign key flight_id.

Here is my database with tables flight and ticket.

enter image description here

Here is my json request (from Mozilla debugger), which I want to save in the database.

enter image description here

You can see flight_id parameter, but I can’t save it to my database, I’ve got such an error:

at [Source: (PushbackInputStream); line: 1, column: 53] (through reference chain: dto.TicketDto["flight_id"]) DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance ofmodel.Flight(although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (199); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance ofmodel.Flight(although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value

Class Ticket:

@Entity
@Table(name = "ticket")
public class Ticket {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int ticket_id;

@Column(name = "place")
private int place;

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

@Column(name = "surname")
private String surname;

@JsonDeserialize(using = CustomParameterDeserializer.class)
@ManyToOne(targetEntity = Flight.class)
@JoinColumn(name = "flight_id")
@JsonBackReference("flight")
private Flight flight_id;

// getters setters
// constructor

Class TicketDto:

public class TicketDto {

private int ticket_id;

private int place;

private String name;

private String surname;

private Flight flight_id;

private Customer customer_id;

// getters setters

My class Flight:

@Entity
@Table(name = "flight")
public class Flight {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int flight_id;

//

@OneToMany(mappedBy = "flight_id")
@JsonManagedReference("flight")
private List<Ticket> ticket;

// getters setters

My class FlightDto:

public class FlightDto {
private int flight_id;

//

private List<TicketDto> ticket;

// getters setters

Here is the class TicketServiceImpl with the method to save my Ticket:

@Service
public class TicketServiceImpl implements TicketService {

@Autowired
TicketRepository repository;

@Override
public Ticket save(TicketDto ticketDto) {
    Ticket ticket = new Ticket();
    ticket.setName(ticketDto.getName());
    ticket.setSurname(ticketDto.getSurname());
    ticket.setCustomer_id(ticketDto.getCustomer_id());
    ticket.setFlight_id(ticketDto.getFlight_id());
    ticket.setPlace(ticketDto.getPlace());

    return repository.save(ticket);
}

My class ticket.ts in Angular:

export class Ticket {
ticket_id: number;
place: string;
customer_id: number;
flight_id: number;
name: string;
surname: string;
}

Advertisement

Answer

It expects a int flight_id in your dto class. Change type of flight_id type to Integer and add another Flight reference in TickerDTO class. Try below to convert it into an Flight object:

public class TicketDto {
private int ticket_id;

private int place;

private String name;

private String surname;

private Flight flight;

private Customer customer_id;

@JsonProperty("flight_id")
private void unpackNested(Integer flight_id) {
    this.flight = new Flight();
    flight.setFlight_id(flight_id);
}

Or if you don’t want to change your ticketDto class change your json to:

{
"name": "name",
"surname": "surname",
"flight_id": {
  "flight_id" : 123
}
}
Advertisement