簡體   English   中英

Spring Boot-從另一個控制器調用一個控制器而不使用RedirectAttributes

[英]Spring Boot - Calling a controller from another controller without using RedirectAttributes

我想做的事:

用戶執行特定操作后,將推送通知發送給管理員。

問題:

如果我使用RedirectAttributes,則需要返回一個字符串值以調用另一個端點,但是我還需要向用戶返回http響應和模型。 所以我想知道是否還有另一種方法可以調用另一個端點,或者使用RedirectAttributes進行調用的正確方法是什么?

@GetMapping("get_admin")
public ResponseEntity<Admin> getAdminById(@RequestHeader("lang") String locale,
                                          @RequestParam("id") Integer id,
                                          RedirectAttributes redirectAttrs) throws EntityNotFoundException {
    Admin admin = adminService.getAdminById(id);
    if (admin == null) {
        return new ResponseEntity(new ResponseAdmin(new LangString().getNoSuchAnAdmin(locale),"0"), HttpStatus.OK);
    }

    redirectAttrs.addFlashAttribute("locale",locale);
    redirectAttrs.addFlashAttribute("opType","0");
    redirectAttrs.addFlashAttribute("message","message");
    redirectAttrs.addFlashAttribute("type","type");
    redirectAttrs.addFlashAttribute("data","data");

    return new ResponseEntity(new ResponseAdmin(admin), HttpStatus.OK); 
    >>> I can't write "return redirect:/push_call;" since return type must be ResponseEntity, not String.
}

@GetMapping("push_call")
public ResponseEntity<String> redirectedPush(Model model){

    int opType = (int) model.asMap().get("opType");;
    String locale = (String) model.asMap().get("locale");;

    String messageStr = (String) model.asMap().get("message");;
    String dataStr = (String) model.asMap().get("data");;
    String typeStr = (String) model.asMap().get("type");;

    JSONObject body = new JSONObject();
    body.put("to", "XXXXXX");

    HttpEntity<String> request = new HttpEntity<>(body.toString());

    CompletableFuture<String> pushNotification = androidPushNotificationsService.send(request);

    try {
        String firebaseResponse = pushNotification.get();
        return new ResponseEntity<>(firebaseResponse, HttpStatus.OK);

    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }

    return new ResponseEntity<>("Push Notification ERROR!", HttpStatus.BAD_REQUEST);
}

只需return redirectedPush(model)然后

暫無
暫無

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

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