java - SpringBoot 说 "Request method ' POST' 不支持

标签 java spring-boot servlet-3.0

我正在使用表单,当我点击“提交”时,它会点击以下 Controller 。

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class ViewController
{
    private final static String USERNAME = "username";
    private final static String PASSWORD = "password";

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String authenticate()
    {
        return "authenticate.html";
    }

@RequestMapping(value = "/connector", method = RequestMethod.POST)
public String connector(final HttpServletRequest request)
{
    final HttpSession session = request.getSession();

    final String username = request.getParameter(USERNAME);
    final String password = request.getParameter(PASSWORD);

    session.setAttribute(USERNAME, username);
    session.setAttribute(PASSWORD, password);
    return "connectors.html";
}
}

我知道该方法正在被命中,因为我在其中放置了断点。但是,我仍然收到上述错误。

编辑:我已经发布了整个 Controller 而不仅仅是方法。
Edit2:我的html表单如下:

    <form action="/connector" method="post" name="authentication_form">
       <input type="text" name="username" id="username">
       <input type="password" name="password" id="password">

        <a href = "javascript:document.authentication_form.submit();" class="link-next">
                    Next
                    <i class="ico-chevron-right"></i>
        </a>
    </form>

我错过了什么?非常感谢任何帮助。

最佳答案

您是否尝试使用 @RestController 注释而不是 @Controller ?

关于java - SpringBoot 说 "Request method ' POST' 不支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34274235/

相关文章:

java - 为什么 java 和 c 中增量运算符的行为不同

java - Spring 启动: Rest endpoint integration with Kafka

java - Spring Boot Rest api

java - 始终向客户端发送 HTTP 响应而无需请求

http - web.xml 中的错误页面配置会转换 404 情况下返回的 HTTP 代码

java - 如何使用 Cayenne 4.0 + PostgreSQL 9.4 管理 PK 生成

java - 如何使用反射通过接口(interface)创建实例?

java - BeanManager 方法 getReference() 在应用程序初始化期间不可用

spring-boot - 将 macOS 从 Big Sur 升级到 Monterey 后,Post 请求出现 403 禁止错误

java - 如何在Eclipse中使用javabean?