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?
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.
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.
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.
ResponseEntity represents the whole HTTP response: status code, headers, and body. As a result, we can use it to fully configure the HTTP response.
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:
"Cannot save file " + fileName
/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";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With