我正在運行 spring-boot 應用程序,我已經將 quartz 調度程序實現為應用程序的一部分。早些時候我有一個帶有端點的休息控制器,例如http://localhost:8080/GoogleMail/ {id} 它觸發如下所示的函數并接受 HttpServletRequest/Response 作為參數以及我傳遞的 Pathvariable。@PostMapping(value = "/GoogleMail/{id}", consumes = "application/json", produces = "application/json") public String sendMail(HttpServletRequest request, HttpServletResponse response, @Valid @PathVariable(value = "id") String id, @Valid @RequestBody MailMessage mailMsg) throws Exception { if(id == null || id.isEmpty()) { ResponseEntity.badRequest().build(); } this.userId = id; return GoogleMailIntegrationService.sendUserMails(request, response, id, mailMsg, m -> !StringUtils.isBlank(mailMsg.getTo()) && !StringUtils.isBlank(mailMsg.getSubject()) && !StringUtils.isBlank(mailMsg.getBody())); }現在不是進行 REST 調用,而是需要使用 Quartz 調度程序每 1 小時發布一次 JSON 正文來調用此函數??赡苋缦聢Dif (context.getJobDetail().getKey().getName().equalsIgnoreCase(JobName.READRESPONSE.toString())) { // emailService.readMail(); try { sendMail(Request, Response, id); } catch (IOException e) { e.printStackTrace(); }我的問題:有沒有辦法使用調度程序進行 REST 調用,或者是否可以通過直接傳遞請求/響應參數來進行 sendMail() 調用。我不確定如何執行此操作,我花了大部分時間在發布之前瀏覽解決方案。
1 回答

偶然的你
TA貢獻1841條經驗 獲得超3個贊
您可以使用RestTemplate通過以下方式對某個控制器端點進行請求調用:
if (context.getJobDetail().getKey().getName().equalsIgnoreCase(JobName.READRESPONSE.toString())) {
// emailService.readMail();
try {
RestTemplate restTemplate = new RestTemplate();
HttpEntity<MailMessage > request = new HttpEntity<>(mailMsg, new HttpHeaders());
ResponseEntity<String> responseEntityStr =
restTemplate.postForEntity(
String.format("http://localhost:7777/GoogleMail/%s", id),
request, String.class);
} catch (IOException e) {
e.printStackTrace();
}
添加回答
舉報
0/150
提交
取消