簡體   English   中英

Spring 數據 REST - 沒有為 POST 方法調用 RepositoryEventHandler 方法?

[英]Spring Data REST - RepositoryEventHandler methods not getting invoked for POST method?

我定義了以下域 object 和 DTO。

國家.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;
}

我已經覆蓋了國家 class 的 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");

}

但是當我向端點http://localhost:8080/countries發送 POST 請求時,不會調用事件處理程序。 我做錯了什么嗎?

更新 1:我正在使用 Postman 將以下 JSON 發送到端點。

"countries":[{
    "countryName":"Australia",
    "countryNationality":"Australian"

}]

不知道您如何調用請求,很難為您提供確切的解決方案。 但是可能的原因是您缺少斜杠符號@RequestMapping值屬性:

@RequestMapping(method = POST, value = "countries")

應該:

@RequestMapping(method = POST, value = "/countries")

在AppConfigration中將Bean定義為

@Configuration
@EnableAsync
public class AppConfig {

  @Bean
    CountryHandler countryHandler (){
        return new CountryHandler ();
    }

}

然后它將起作用。

嘗試從以下位置編輯Controller類注釋:

@RepositoryRestController

@RestController

主要是來自以下方法的方法注釋:

@RequestMapping(method = POST, value = "countries")

@RequestMapping(value = "/countries", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)

PS:如果要返回json,則produces = MediaType.APPLICATION_JSON_VALUE

我知道這是舊的,但它按預期工作。

@RepositoryRestController實現中定義的方法替換默認RepositoryEntityController中發布@RepositoryEventHandler事件的方法。

所以你的 controller 需要發布一個創建事件:

@RepositoryRestController
public class CountryController {

    @Autowired
    private CountryRepository repo;

    private final ApplicationEventPublisher publisher; //This changed

    @RequestMapping(method = POST, value = "countries")
    public @ResponseBody ResponseEntity<?> createCountry(@RequestBody Resource<CountryDTO> dto,
            Pageable page, PersistentEntityResourceAssembler resourceAssembler) {

        Country savedCountry = repo.save(dto.getContent().getCountries());
        publisher.publishEvent(new BeforeCreateEvent(savedCountry)); //This changed
        return new ResponseEntity<>(resourceAssembler.toResource(savedCountry), HttpStatus.OK);
    }


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM