java - 重定向到同一 Controller 中的 POST 方法

标签 java rest spring-mvc redirect

我有一个 Spring RestController,并且想要使用 RequestBody 重定向到同一 Controller 中的 POST 方法。 欢迎任何解决方案,而不仅仅是重定向。

我的 Controller :

@RequestMapping(value = "/addCompany", method = RequestMethod.POST)
public String addCompany(@Valid Company company, BindingResult result,
        HttpServletRequest request, Model model) throws Exception {
    //some logic
    //need to pass Company Object as RequestBody
    return "redirect:/app/postmethod/";
}

//Method to redirected
@RequestMapping(value = "/postmethod", method = {RequestMethod.POST, RequestMethod.GET})
public String getData( @RequestBody(required=false) Company compnay, HttpServletRequest request, Model model) throws Exception {
    //some logic
    //required company object
    return "htmlpage";
}

我需要将我的请求从同一 Controller 中的 addCompany 方法重定向到 /postmethod,我愿意使用任何可行的解决方案。

最佳答案

在这里检查: https://www.baeldung.com/spring-redirect-and-forward#redirecting-an-http-post-request

As per HTTP 1.1 protocol reference, status codes 301 (Moved Permanently) and 302 (Found) allow the request method to be changed from POST to GET. The specification also defines the corresponding 307 (Temporary Redirect) and 308 (Permanent Redirect) status codes that don't allow the request method to be changed from POST to GET.

@PostMapping("/redirectPostToPost")
public ModelAndView redirectPostToPost(HttpServletRequest request) {
    request.setAttribute(
      View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.TEMPORARY_REDIRECT);
    return new ModelAndView("redirect:/redirectedPostToPost");
}

@PostMapping("/redirectedPostToPost")
public ModelAndView redirectedPostToPost() {
    return new ModelAndView("redirection");
}

请求正文将被传递。这是使用您的代码的示例:

@RestController
@RequestMapping("app")
public class TestController {

    @PostMapping("/addCompany")
    public ModelAndView addCompany(@RequestBody Company company, HttpServletRequest request) {
        System.out.println("First method: " + company.name);
        request.setAttribute(
                View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.TEMPORARY_REDIRECT);
        return new ModelAndView("redirect:/app/postmethod/");
    }

    @PostMapping("/postmethod")
    public void getData(@RequestBody Company company) {
        System.out.println("Redirected: " + company.name);
    }

    public static class Company {
        String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}

当使用 POST 请求到 http://localhost:8080/app/addCompany 时,主体为 {"name": "Test Company"},在输出中我接收下一个:

First method: Test Company
Redirected: Test Company

关于java - 重定向到同一 Controller 中的 POST 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60431441/

相关文章:

java - 按显示顺序获取组件

java - Spring MVC 随着页面方向的改变而改变语言环境

java - 使用 RESTeasy 管理属性继承

api - REST API 身份验证 - 这足够吗?

java - 从 View 返回时,Session 属性返回 null

spring - Spring MVC错误404错误请求Kotlin

java - 封装 JComboBox 以确保字段为空

java - Safari 中的 HTML5 视频无法正常工作

使用 Java 8 在 Glassfish 上运行的 Spring、Hibernate 和 JSF 项目中的 java.lang.NoSuchMethodError : sun. security.ssl.SSLSessionImpl.<init>

wcf - 可以从 SOAP UI 或任何此类工具设置 HTTP Origin 和 Referer 值吗?