2
を使用せずに、別のコントローラからコントローラを呼び出す:春ブーツ - 私は何をしたいのか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);
}
何が "モデル" の略でしょうか?管理者モデルですか? – ACAkgul
モデルを「モデル」にキャストできたかどうかはわかりませんでしたが、とにかく試しました。予想通りこのエラーが発生しました。管理者はorg.springframework.ui.Modelにキャストすることはできません – ACAkgul
新しいモデルを作成し、それは 'optType、locale、message ...'などになります。 –