So I'm getting an warning as
WARN [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (default task-2) Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]
when I go to the following URL http://localhost:8080/ProjectFE/uregistration
and the website shows
HTTP 405 Method not allowed
here is my controller code:
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import model.daoimpl.UserinfoDaoImpl;
import model.dao.IUserinfoDAO;
import model.entity.Userinfo;
@Controller
public class RegistrationController {
@RequestMapping(value="/registration",method = RequestMethod.GET)
public String addRegistrationPage() {
return "registrationpage";
}
@RequestMapping(value="/uregistration",method = RequestMethod.POST)
public String addURegistrationPage(@ModelAttribute("User")Userinfo u) {
IUserinfoDAO iu = new UserinfoDaoImpl();
boolean b = iu.insertInfo(u);
if(b)
return "success";
else
return "registrationpage";
}
}
So what should I do ? Also if any other code is required please comment I'll edit the post, Thankyou.
I solved mine by simply adding csrf configurations as mentioned in spring documentation when making an ajax request to the server. Remember that csrf is enabled by default in spring and you have to either disable it (bad) or enable it. Link is here
So I added
<meta name="_csrf" th:content="${_csrf.token}" /> <meta name="_csrf_header" th:content="${_csrf.headerName}" />
to my html head and inside the form I am submitting, I included this little hidden input and mine worked for me. Hope it helps ya.
The issue is with the annotation
@Controller
inside your code. change it to@RestController
and it should start working. Here is the Sample code which I have created and it works well :As your
GET
handler mapping is/registration
, change yourPOST
handler mapping to same value: