简体   繁体   中英

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?

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM