gstackoverflow
gstackoverflow

Reputation: 36984

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?

Upvotes: 24

Views: 56158

Answers (3)

JTP
JTP

Reputation: 231

This could 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);
}

In order for Spring to handle the error in your @ExceptionHandler method, it's required that you have the org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver exception resolver enabled. However, if you haven't specified any custom exception resolvers it will be enabled by default.

Upvotes: 6

Serge Ballesta
Serge Ballesta

Reputation: 148965

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";
    }

Upvotes: 5

Jordi Castilla
Jordi Castilla

Reputation: 26961

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

Upvotes: 39

Related Questions