MaxPower
MaxPower

Reputation: 871

SpringBoot says "Request method 'POST' not supported

I am using a form, and when I hit submit it hits the following 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";
}
}

I know the method is being hit as I have placed breakpoints in it. However, I still get the above mentioned error.

Edit: I have posted the entire controller rather than just the method.
Edit2: My html form is as follows:

    <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>

What am I missing? Any help is greatly appreciated.

Upvotes: 0

Views: 931

Answers (2)

MaxPower
MaxPower

Reputation: 871

I had to create another method that uses GET and redirect to it.

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

Upvotes: 0

Eria
Eria

Reputation: 3182

Did you try using @RestController annotation instead of @Controller ?

Upvotes: 1

Related Questions