【问题标题】:Spring MVC 'forward' not works with @PostMapping and @GetMapping Annotations for same URISpring MVC 'forward' 不适用于同一 URI 的 @PostMapping 和 @GetMapping 注解
【发布时间】:2020-07-30 09:52:50
【问题描述】:

我已经为 /login 端点使用了@PostMapping 和@GetMapping,如下所示,

对于验证失败后的 /login 页面,如果我转发到带有错误消息的相同 /login 页面,它将无法正常工作并抛出 405 状态的方法不支持错误。

当我先切换@PostMapping 然后切换@GetMapping 时,/login 页面本身未加载(请参阅之前部分)。

我已经用普通的 RequestMapping 参数(转发和普通登录)修复了这个问题,但想更多地了解这些注释的内部机制,它是如何工作的。

之前:

@GetMapping(value = "/login")
@PostMapping(value = "/login")
public ModelAndView getLoginPage() {
    ModelAndView modelAndView = new ModelAndView(LOGIN_VIEW);
    return modelAndView;
}

之后:

@RequestMapping(value = "/login", method = { GET, POST })
public ModelAndView getLoginPage() {
    ModelAndView modelAndView = new ModelAndView(LOGIN_VIEW);
    return modelAndView;
}

【问题讨论】:

    标签: java spring spring-mvc spring-annotations


    【解决方案1】:

    @GetMapping 等只是相应@RequestMapping 的别名。所以你基本上有 2 个@RequestMappings。在这种情况下,第二个被忽略。在你的 after 块中,你做对了,并且在一个 single @RequestMapping 中有多个映射。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-07
      • 2022-11-13
      • 2021-01-24
      • 2022-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-07
      相关资源
      最近更新 更多