Vio*_*ola 9 java json foreign-keys angular
我有两个班级Flight和Ticket. 一个航班可以有很多票,所以Ticket有外键flight_id。
这是我的数据库,其中包含表flight和ticket.
这是我的json request(来自 Mozilla 调试器),我想将其保存在数据库中。
您可以看到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
Run Code Online (Sandbox Code Playgroud)
类票证Dto:
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
Run Code Online (Sandbox Code Playgroud)
我的班级航班:
@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
Run Code Online (Sandbox Code Playgroud)
我的班级 FlightDto:
public class FlightDto {
private int flight_id;
//
private List<TicketDto> ticket;
// getters setters
Run Code Online (Sandbox Code Playgroud)
这是带有保存我的票证的方法的 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);
}
Run Code Online (Sandbox Code Playgroud)
我在 Angular 中的类 ticket.ts:
export class Ticket {
ticket_id: number;
place: string;
customer_id: number;
flight_id: number;
name: string;
surname: string;
}
Run Code Online (Sandbox Code Playgroud)
Adi*_*xit 11
它需要您的 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);
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您不想更改您的 ticketDto 类,请将您的 json 更改为:
{
"name": "name",
"surname": "surname",
"flight_id": {
"flight_id" : 123
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19502 次 |
| 最近记录: |