Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use spring redirect 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 behaviour - redirect to the controller:

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

But I cannot write it because "redirect:/member/uploadImage" is String but should be ResponseEntity

How can I resolve my problem?

like image 667
gstackoverflow Avatar asked Aug 24 '15 13:08

gstackoverflow


People also ask

How do I redirect a Spring boot controller?

Try a URL http://localhost:8080/HelloWeb/index and you should see the following result if everything is fine with your Spring Web Application. Click the "Redirect Page" button to submit the form and to get the final redirected page.

How use redirect attribute in Spring MVC?

You can use RedirectAttributes to store flash attributes and they will be automatically propagated to the "output" FlashMap of the current request. A RedirectAttributes model is empty when the method is called and is never used unless the method returns a redirect view name or a RedirectView.

What is the difference between ResponseBody and ResponseEntity?

ResponseEntity represents an HTTP response, including headers, body, and status. While @ResponseBody puts the return value into the body of the response, ResponseEntity also allows us to add headers and status code.

What does ResponseEntity OK () do?

ResponseEntity represents the whole HTTP response: status code, headers, and body. As a result, we can use it to fully configure the HTTP response.


2 Answers

If you don't explicity need to return a ResponseEntity you can redeclare your method like:

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 seems 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);
like image 178
Jordi Castilla Avatar answered Oct 15 '22 17:10

Jordi Castilla


Spring controller method return values are just sugar when you want the output from the controller to be post processed by Spring machinery. If I have correctly understood what you are doing, you have only 2 possibilities:

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

As Spring provides more goodies for redirect than for SendError, my advice would be to have you method 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";
    }
like image 26
Serge Ballesta Avatar answered Oct 15 '22 16:10

Serge Ballesta