[英]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
但我无法写它,因为
"redirect:/member/uploadImage"
是String,但应该是ResponseEntity
How can I resolve my problem? 我该如何解决我的问题?
If you don't explicity need to return a ResponseEntity
you can redeclare your method like: 如果您没有明确需要返回
ResponseEntity
您可以重新声明您的方法,如:
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 . 但是,如果你需要使用
ResponseEntity
的话,好像你可以重定向添加到ResponseEntity
描述这里 。
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. 当您希望控制器的输出由Spring机器进行后处理时,Spring控制器方法返回值只是糖。 If I have correctly understood what you are doing, you have only 2 possibilities:
如果我正确理解你在做什么,你只有两种可能:
"Cannot save file " + fileName
"Cannot save file " + fileName
/member/uploadImage
in the same application context. /member/uploadImage
。 As Spring provides more goodies for redirect than for SendError
, my advice would be to have you method return a string: 由于Spring为重定向提供了比
SendError
更多的好东西,我的建议是让你的方法返回一个字符串:
@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. 这可以使用
@ExceptionHandler
注释来完成。
@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. 为了让Spring在
@ExceptionHandler
方法中处理错误,需要启用org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver
异常解析器。 However, if you haven't specified any custom exception resolvers it will be enabled by default. 但是,如果您尚未指定任何自定义异常解析程序,则默认情况下将启用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.