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:

JavaScript

Class TicketDto:

JavaScript

My class Flight:

JavaScript

My class FlightDto:

JavaScript

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

JavaScript

My class ticket.ts in Angular:

JavaScript

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:

JavaScript

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

JavaScript
Advertisement