Mam taką metodę z @GetMapping
jak widać poniżej i przy
GET http://localhost:8080/api/taxoffice/971/details w Postman zwraca error 400.
Natomiast jeśli sprobuję GET http://localhost:8080/api/taxoffice/101/details
to zwraca
error 500 "Cannot invoke
"com.walidatorpnr.taxoffice.service.TaxOffice_DetailService.findAllByTaxOfficeID(int)"
because "this.taxOfficeDetailService" is null".
Rekordy z powyższymi id istnieją w bazie.
Co powoduje ten błąd? Czy może to mieć związek z polami, które mają adnotacje @OneToMany
itp ?
RestController
@RestController
@RequestMapping("api/taxoffice")
public class TaxOfficeRestController {
private final TaxOfficeService taxOfficeService;
private final TaxOffice_DetailService taxOfficeDetailService;
@Autowired
public TaxOfficeRestController(TaxOfficeService taxOfficeService, TaxOffice_DetailService taxOfficeDetailService) {
this.taxOfficeService = taxOfficeService;
this.taxOfficeDetailService = taxOfficeDetailService;
}
List<TaxOffice> taxOffices = Lists.newArrayList();
List<TaxOffice_Detail> taxOffice_details = Lists.newArrayList();
//Endpoint, z którym jest
@GetMapping(value = "/{taxOffice_id}/details")
private ResponseEntity<?> getTaxOfficeDetailsByTaxOfficeId(@PathVariable int taxOffice_id) {
taxOffice_details = taxOfficeDetailService.findAllByTaxOfficeID(taxOffice_id);
if(taxOffice_details.size() != 0) {
for (TaxOffice_Detail taxOfficeDetail : taxOffice_details) {
Link selfLink = linkTo(methodOn(TaxOfficeRestController.class).getTaxOfficeDetailsByTaxOfficeId(taxOffice_id)).withSelfRel();
// Link relLink = linkTo(methodOn(TaxOfficeRestController.class).getTaxOfficeById(taxOffice_id)).withRel("Urząd skarbowy");
taxOfficeDetail.add(selfLink);
// taxOfficeDetail.add(relLink);
}
Link link = linkTo(methodOn(TaxOfficeRestController.class).getTaxOfficeDetailsByTaxOfficeId(taxOffice_id)).withSelfRel();
CollectionModel<TaxOffice_Detail> result = CollectionModel.of(taxOffice_details, link);
return ResponseEntity.ok(result);
}
else{
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
}
TaxOffice_Detail.java(nie wklejam setterów i getterów)
@Entity
@Table(name = "TaxOffice_Details")
public class TaxOffice_Detail extends RepresentationModel<TaxOffice_Detail> {
public TaxOffice_Detail() {}
public TaxOffice_Detail(int id, String street, String zipcode, String city, String phone, String fax, String email, String www) {
this.id = id;
this.street = street;
this.zipcode = zipcode;
this.city = city;
this.phone = phone;
this.fax = fax;
this.email = email;
this.www = www;
}
@Id
private int id;
@Column(name = "street")
private String street;
@Column(name = "zipcode")
private String zipcode;
@Column(name = "city")
private String city;
@Column(name = "phone")
private String phone;
@Column(name = "fax")
private String fax;
@Column(name = "email")
private String email;
@Column(name = "www")
private String www;
@Transient
private String taxoffice_name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "taxOffice_id")
private TaxOffice taxOffice;
// @JsonIgnore
@OneToMany(fetch = FetchType.LAZY, mappedBy = "taxOffice_details")
private List<TaxOffice> taxOffices;
}
TaxOffice_DetailRepository
@Repository("taxOffice_DetailRepository")
public interface TaxOffice_DetailRepository extends JpaRepository<TaxOffice_Detail, Integer> {
@Query("select t from TaxOffice_Detail t where t.id = :id")
List<TaxOffice_Detail> findAllById(int id);
@Query("select t from TaxOffice_Detail t where t.taxOffice.id = :taxOffice_id")
List<TaxOffice_Detail> findAllByTaxOfficeId(int taxOffice_id);
}
TaxOffice_DetailService
@Transactional
@Service
public class TaxOffice_DetailService {
final
TaxOffice_DetailRepository taxOffice_detailRepository;
public TaxOffice_DetailService(TaxOffice_DetailRepository taxOffice_detailRepository) {
this.taxOffice_detailRepository = taxOffice_detailRepository;
}
public List<TaxOffice_Detail> findAllByID(int id) {
return taxOffice_detailRepository.findAllById(id);
}
public List<TaxOffice_Detail> findAllByTaxOfficeID(int taxOffice_id) {
return taxOffice_detailRepository.findAllByTaxOfficeId(taxOffice_id);
}
}