我有以下域對象和DTO定義。Spring Data REST - RepositoryEventHandler方法沒有被POST方法調用?
Country.java
@Data
@Entity
public class Country extends ResourceSupport {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long countryID;
@NotBlank(message = "Country name is a required field")
private String countryName;
private String countryNationality;
}
CountryDTO.java
@Data
public class CountryDTO {
private List<Country> countries;
}
我已經覆蓋在RepositoryRestController爲國類POST方法。
@RepositoryRestController
public class CountryController {
@Autowired
private CountryRepository repo;
@RequestMapping(method = POST, value = "countries")
public @ResponseBody ResponseEntity<?> createCountry(@RequestBody Resource<CountryDTO> dto,
Pageable page, PersistentEntityResourceAssembler resourceAssembler) {
Country savedCountry = repo.save(dto.getContent().getCountries());
return new ResponseEntity<>(resourceAssembler.toResource(savedCountry), HttpStatus.OK);
}
}
現在我已經定義了一個RepositoryEventHandler來處理驗證。
@Component
@RepositoryEventHandler
public class CountryHandler {
@HandleBeforeCreate
public void handleBeforeCreate(Country country) {
System.out.println("testing");
}
但是,當我發送POST請求到端點http://localhost:8080/countries
,該事件處理程序不被調用。有什麼我做錯了嗎?
UPDATE 1: 我使用Postman將以下JSON發送到端點。
"countries":[{
"countryName":"Australia",
"countryNationality":"Australian"
}]
你如何在該URL上調用POST? – DaveRlz
我通過郵遞員發送JSON –