SpringBoot说“不支持请求方法'POST'

时间:2015-12-14 18:38:55

标签: java spring-boot servlet-3.0

我正在使用表单,当我点击提交时,它会点击以下控制器。

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";
}
}

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

编辑:我发布了整个控制器,而不仅仅是方法 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>

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我必须创建另一个使用GET并重定向到它的方法。

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

    return "redirect:/test";
}

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