I have two microservices(for now) Movie, Show
MovieEntity.java
@Entity
@Table(name = "MOVIES")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Movie {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(nullable = false)
private String movieName;
private Double duration;
@Column(scale = 2)
private Double rating;
private Date releaseDate;
@Enumerated(value = EnumType.STRING)
private Genre genre;
@Enumerated(value = EnumType.STRING)
private Language language;
@OneToMany(mappedBy = "movie",cascade = CascadeType.ALL)
@JsonManagedReference
private List<Show> shows = new ArrayList<>();
}
MovieService.java
public void addShowToMovie(Integer movieId, Show show) {
Movie movie = movieRepository.findById(movieId)
.orElseThrow(() -> new MovieDoesNotExistsException(movieId));
//Show show = Transformer.showDtoToShow(showEntryDto);
if (movie.getShows() == null) {
movie.setShows(new ArrayList<>());
}
show.setMovie(movie); // Ensure it's linked
movie.getShows().add(show);
movieRepository.save(movie);
}
Similarly in Show microservice,
@Entity
@Table(name = "SHOWS")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Show {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer showId;
private Time time;
private Date date;
@ManyToOne
@JoinColumn
@JsonBackReference
private Movie movie;
@OneToMany(mappedBy = "show", cascade = CascadeType.ALL)
private List<ShowSeat> showSeatList = new ArrayList<>();
@OneToMany(mappedBy = "show", cascade = CascadeType.ALL)
private List<Ticket> ticketList = new ArrayList<>();
@ManyToOne
@JoinColumn
private Theater theater;
}
ShowService.java
public String addShow(ShowEntryDto showEntryDto) {
// Validate movieId
Movie movie = getMovieById(showEntryDto.getMovieId());
Show show = Transformer.showDtoToShow(showEntryDto);
show.setMovie(movie);
// Step 1: Save Show in Show Microservice
show = showRepository.save(show);
try {
// Step 2: Call Movie Microservice to link Show
apiGatewayClient.addShowToMovie(movie.getId(), show);
} catch (Exception e) {
// Step 3: Rollback (Delete the Show) if API call fails
e.printStackTrace();
showRepository.delete(show);
throw new RuntimeException("Failed to link Show to Movie. Rolling back Show creation.", e);
}
return "Show has been added Successfully";
}
private Movie getMovieById(Integer movieId) {
try {
return apiGatewayClient.getMovie(movieId).getBody();
} catch (FeignException.NotFound e) {
System.out.println("Movie not found: " + movieId);
throw new MovieDoesNotExistsException(movieId);
} catch (FeignException e) {
System.out.println("Movie Service error: " + e.getMessage());
throw new RuntimeException("Movie Service unavailable");
}
}
While calling /show/addShow controller, though I am able to save the show first, In movie service while linking the show to movie and save, its throwing below error
feign.FeignException$InternalServerError: [500] during [POST] to [http://MOVIE-SERVICE/movie/1/shows] [ApiGatewayClient#addShowToMovie(Integer,Show)]: [{"status":500,"error":"Internal Server Error","message":"Cannot invoke \"java.util.List.size()\" because \"this.this$0.operationQueue\" is null","path":"/movie/1/shows","timestamp":"2025-03-23T17:19:12.090776"}] at feign.FeignException.serverErrorStatus(FeignException.java:281) at feign.FeignException.errorStatus(FeignException.java:226)
I am new to springboot and microservices architecture, I want to know what is the issue here and apart from using DTO(which has its own drawbacks) and saving List of showId, what is the best way to implement saving of show and linking movie to the show(with casecade preferably).
Thanks, Debasish
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744287697a4566878.html
评论列表(0条)