How to use spring redirection if controller method returns ResponseEntity

I want to write something like this:

@RequestMapping(value = { "/member/uploadExternalImage", "/member/uploadExternalImage" }, method = RequestMethod.GET) public ResponseEntity<String> handleFileUpload(@RequestParam String url,@RequestParam String fileName, RedirectAttributes redirectAttributes) { ... return new ResponseEntity("Cannot save file " + fileName, HttpStatus.INTERNAL_SERVER_ERROR); ... return "redirect:/member/uploadImage"; } 

expected behavior - redirect to controller:

 @RequestMapping(value = { "/member/createCompany/uploadImage", "/member/uploadImage" }) @ResponseBody public ResponseEntity<String> handleFileUpload(@Validated MultipartFileWrapper file, BindingResult result, Principal principal 

But I can not write it, because "redirect:/member/uploadImage" is a string, but must be ResponseEntity

How can I solve my problem?

+5
source share
3 answers

Spring The return values โ€‹โ€‹of the controller method are just sugar when you want the controller output to be processed after processing by the Spring machine. If I understand correctly what you are doing, you have only two options:

  • send an error response with the code 500 and the message "Cannot save file " + fileName
  • redirect to /member/uploadImage in the same application context.

Since Spring provides more benefits for redirection than SendError , my advice would be for the method to return a string:

 @RequestMapping(value = { "/member/uploadExternalImage", "/member/uploadExternalImage" }, method = RequestMethod.GET) public String handleFileUpload(@RequestParam String url, @RequestParam String fileName, RedirectAttributes redirectAttributes, HttpServletResponse resp) { ... //return new ResponseEntity("Cannot save file " + fileName, HttpStatus.INTERNAL_SERVER_ERROR); resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Cannot save file " + fileName); // explicitely put error message in request return null; // return null to inform Spring that response has already be processed ... return "redirect:/member/uploadImage"; } 
+3
source

If you do not want to return a ResponseEntity , you can reuse your method, for example:

 public String handleFileUpload(@RequestParam String url,@RequestParam String fileName, RedirectAttributes redirectAttributes) { return "Cannot save file " + fileName; ... return "redirect:/member/uploadImage"; } 

But if you need to use ResponseEntity , then it looks like you can add a redirect to ResponseEntity , as described here .

 HttpHeaders headers = new HttpHeaders(); headers.add("Location", "/member/uploadImage"); return new ResponseEntity<String>(headers,HttpStatus.FOUND); 
+9
source

This can be done using the @ExceptionHandler annotation.

  @RequestMapping(value = { "/member/uploadExternalImage", "/member/uploadExternalImage" }, method = RequestMethod.GET) public String handleFileUpload(@RequestParam String url, @RequestParam String fileName, RedirectAttributes redirectAttributes) throws FileUploadException { ... throw new FileUploadException("Cannot save file " + fileName); ... return "redirect:/member/uploadImage"; } @ExceptionHandler(FileUploadException.class) ResponseEntity<String> handleFileUploadError(final FileUploadException e) { return new ResponseEntity(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } 

For Spring to handle the error in your @ExceptionHandler method, this requires that you org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver exception org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver . However, if you did not specify any custom exceptions, they will be enabled by default.

0
source

All Articles