简体   繁体   English

如果controller方法返回ResponseEntity,如何使用spring重定向

[英]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: 如果我正确理解你在做什么,你只有两种可能:

  • send an error response with code 500 and message "Cannot save file " + fileName 发送代码为500的错误响应和消息"Cannot save file " + fileName
  • redirect to /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.

相关问题 Spring Web MVC:如何在视图类而不是控制器中使用ResponseEntity? - Spring Web MVC: How to use a ResponseEntity in the view class, not the controller? 如何测试返回 Mono 的控制器<ResponseEntity<Void> &gt;? - How to test Controller that returns Mono<ResponseEntity<Void>>? spring控制器返回带有post的404和带有get的文件以生成响应性 - spring controller returns 404 with post and file with get to generate responseentity 当控制器返回ResponseEntity时,如何在Filter中设置响应状态代码? - How to set response status code in Filter when controller returns ResponseEntity? 如何对spring mvc控制器发送的ResponseBody或ResponseEntity进行单元测试? - How to unit test a ResponseBody or ResponseEntity sent by a spring mvc Controller? 如果方法返回ResponseEntity,如何重定向到外部url <Object> - How to redirect to external url if method return ResponseEntity<Object> Spring MVC - 如何在 ResponseEntity 方法中返回视图? - Spring MVC - How do I return a view in a ResponseEntity method? 如何重定向到部署在本地服务器上的应用程序中的spring controller方法? - How to redirect to spring controller method in app deployed on local server? 不使用ResponseEntity时Spring控制器不返回值 - Spring controller not returning value when not using ResponseEntity 如何编写返回图像的 Spring Controller 方法? - How do I write a Spring Controller method that returns an image?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM