如何从Number值反序列化为int(错误-没有从Number值反序列化的int / Int参数构造函数/工厂方法)

时间:2018-12-17 15:31:45

标签: java json angular foreign-keys

我有两个班FlightTicket。一次航班可以有很多票,因此Ticket具有外键flight_id

这是我的数据库,其中包含表flightticket

enter image description here

这是我的json request(来自Mozilla调试器),我想将其保存在数据库中。

enter image description here

您可以看到flight_id参数,但是我无法将其保存到数据库中,但出现了这样的错误:

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 of模型。飞行(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 of模型。飞行(although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value

机票:

@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

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

我班的航班:

@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

我的班机FlightDto:

public class FlightDto {
private int flight_id;

//

private List<TicketDto> ticket;

// getters setters

这是TicketServiceImpl类,其中包含保存我的票证的方法:

@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);
}

我在Angular中的班票.ts

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

2 个答案:

答案 0 :(得分:2)

它在您的dto类中期望一个int flight_id。将flight_id类型的类型更改为Integer,然后在TickerDTO类中添加另一个Flight参考。请尝试以下将其转换为Flight对象:

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);
}

或者如果您不想更改ticketDto类,请将json更改为:

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

答案 1 :(得分:0)

您需要使用@JsonCreator在Flight中创建构造函数:

@JsonCreator
public Flight (@JsonProperty("flight_id") Integer flight_id ) {
    this.flight_id = flight_id;
}