Класс сущности
@Entity
@Table(name = "matches")
public class Matches extends RepresentationModel<Matches> {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer matchId;
private Integer season;
private String city;
@Temporal(TemporalType.TIMESTAMP)
private Date date;
private String team1;
private String team2;
private String tossWin;
private String tossDecision;
private String result;
private String winner;
@OneToMany(mappedBy = "matches", cascade = CascadeType.ALL)
private Set<Deliveries> deliveries;
public Matches() {
}
public Matches(Integer matchId, Integer season, String city, Date date, String team1, String team2,
String tossWin, String tossDecision, String result, String winner) {
super();
this.matchId = matchId;
this.season = season;
this.city = city;
this.date = date;
this.team1 = team1;
this.team2 = team2;
this.tossWin = tossWin;
this.tossDecision = tossDecision;
this.result = result;
this.winner = winner;
}
// getters and settes
}
Хранилище
@Repository
public interface MatchesRepository extends JpaRepository<Matches, Integer> {
@Query(value = "FROM Matches m")
public List<Matches> findByMatchId(Integer matchId);
}
Служба
@Service
public class MatchesService {
@Autowired
private MatchesRepository matchRepository;
public List<Matches> getMatchDetailsByMatchId(Integer matchId) {
List<Matches> matchesList = matchRepository.findByMatchId(matchId);
return matchesList;
}
}
Контроллер
@RestController
@RequestMapping("/matches")
public class MatchesController {
@Autowired
private MatchesService matchesService;
@GetMapping("/{matchId}")
public ResponseEntity<Object> getAllByMatchId(@PathVariable Integer matchId) {
List<Matches> matchDetailsBySeason = matchesService.getMatchDetailsByMatchId(matchId);
Link link = WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(MatchesController.class)).slash(matchId)
.withSelfRel();
CollectionModel<Matches> matches = new CollectionModel<Matches>(matchDetailsBySeason, link);
return ResponseEntity.ok(matches);
}
}
Когда я запрашиваю записи на основе идентификатора совпадения, я получаю NullPointerException
. Здесь я прилагаю URL и исключение
URL -> http://localhost: 8080 / совпадений / 1
Исключение ->
Hibernate: select matches0_.match_id as match_id1_1_, matches0_.city as city2_1_, matches0_.date as date3_1_, matches0_.result as result4_1_, matches0_.season as season5_1_, matches0_.team1 as team6_1_, matches0_.team2 as team7_1_, matches0_.toss_decision as toss_dec8_1_, matches0_.toss_win as toss_win9_1_, matches0_.winner as winner10_1_ from matches matches0_
2020-04-30 19:04:24.565 ERROR 48488 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at org.springframework.hateoas.server.core.WebHandler.linkTo(WebHandler.java:113) ~[spring-hateoas-1.0.4.RELEASE.jar:1.0.4.RELEASE]
at org.springframework.hateoas.server.core.WebHandler.linkTo(WebHandler.java:95) ~[spring-hateoas-1.0.4.RELEASE.jar:1.0.4.RELEASE]
at org.springframework.hateoas.server.mvc.WebMvcLinkBuilderFactory.linkTo(WebMvcLinkBuilderFactory.java:109) ~[spring-hateoas-1.0.4.RELEASE.jar:1.0.4.RELEASE]
at org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.java:171) ~[spring-hateoas-1.0.4.RELEASE.jar:1.0.4.RELEASE]
at com.learning.ipl.controller.MatchesController.getAllByMatchId(MatchesController.java:47) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]