发布不支持的MediaType

时间:2018-07-10 15:15:32

标签: java angular spring-boot

当我调用上述方法时,我收到错误415 (Unsupported Media Type)

角形

  this.reservationService.createReservation(this.reservation).subscribe(res =>{
    console.log(res);
  });

预订是ReservationD类型

export class ReservationDto {
    email: string;
    firstName: string;
    lastName: string;
    phoneNumber: string;
    reservationDate: string;
    id_seance: number;
    user_id: number;
}

在Reservation.service中

  public createReservation(reservationDTO: ReservationDto) {
    return this.http.post(ReservationService.RESERVATION_URL + ReservationService.CREATE_RESERVATION, ReservationService.CREATE_RESERVATION);
  }

在Java中 我的实体

@Entity
@Table(name="reservation")
@Data
public class Reservation {

    public Reservation() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false, unique = true)
    private Integer id;

    private java.time.LocalDate reservationDate;

    @NonNull
    private String email;

    @NonNull
    private String firstName;

    @NonNull
    private String lastName;

    @Column(length=9)
    @NonNull
    private String phoneNumber;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "id_seance")
    private Seance reservationSeance;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "user_id")
    private User userReservation;

    @ManyToMany(mappedBy = "reservations")
    private List<Seat> seats = new ArrayList<>();
}

服务

@Transactional
public Reservation createReservation(ReservationDTO reservationDTO) {
    Reservation reservation = new Reservation();
    reservation.setEmail(reservationDTO.getEmail());
    reservation.setFirstName(reservationDTO.getFirstName());
    reservation.setLastName(reservationDTO.getLastName());
    reservation.setPhoneNumber(reservationDTO.getPhoneNumber());
reservation.setReservationDate(LocalDate.parse(reservationDTO.getReservationDate()));
    return reservationRepository.saveAndFlush(reservation);
}

和控制器

@RequestMapping(value = "/createEvent", method = RequestMethod.POST)
public @ResponseBody
Reservation createReservation(@RequestBody ReservationDTO reservationDTO) {
    return reservationService.createReservation(reservationDTO);
}

我尝试在Service.java中将行添加到数据库中,而不传递整个字段。我的问题是我必须手动设置id保留,否则可以自动完成?我在另一个用户的实体字段中有Seance,并且必须在整个对象中都找到findById()或只需要ID。以及如何避免出现错误415。

0 个答案:

没有答案