2017-11-24 1 views
2

Was ich tun möchte:Frühling Boot - Aufruf einer Steuerung von einem anderen Controller ohne RedirectAttributes mit

Senden Push-Benachrichtigung auf Admin, nachdem ein Benutzer eine bestimmte Sache zu tun.

Problem:

Wenn ich RedirectAttributes verwenden, ich brauche einen String-Wert zurückzukehren und eine anderen Endpunkts für den Aufruf aber ich brauche auch eine HTTP-Antwort und ein Modell Benutzer zurückzukehren. Also frage ich mich, ob es eine andere Möglichkeit gibt, einen anderen Endpunkt aufzurufen oder wie man das mit RedirectAttributes macht.

@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); 
} 

Antwort

0

Gerade return redirectedPush(model) dann

+0

was bedeutet "Modell" steht für? Admin-Modell? – ACAkgul

+0

Ich war mir nicht sicher, ob ich irgendein Modell auf "Model" werfen könnte, aber ich habe es trotzdem versucht: Ich habe diesen Fehler wie erwartet erhalten: Admin kann nicht in org.springframework.ui.Model umgewandelt werden – ACAkgul

+0

Sie können ein neues Modell erstellen und die erwarteten Werte übergeben in es 'optType, locale, Nachricht ...' usw. –

Verwandte Themen