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.
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 :
package com.sample;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@RestController
public class Sample {
@RequestMapping(value="/registration",method = RequestMethod.GET)
public String addRegistrationPage() {
return "registrationpage";
}
@RequestMapping(value="/uregistration",method = RequestMethod.POST)
public String addURegistrationPage(@RequestBody String u) {
boolean b = true;
if (b)
return "success";
else
return "registra";
}
}
As your GET
handler mapping is /registration
, change your POST
handler mapping to same value:
@RequestMapping(value = "/registration", method = RequestMethod.POST)
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
https://docs.spring.io/spring-security/site/docs/5.0.x/reference/html/csrf.html
So I added
<meta name="_csrf" th:content="${_csrf.token}" />
<meta name="_csrf_header" th:content="${_csrf.headerName}" />
<script type="application/x-javascript">
(function () {
var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
$(document).ajaxSend(function(e, xhr, options) {
xhr.setRequestHeader(header, token);
});
});
</script>
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.
<input type="hidden" th:name="${_csrf.parameterName}"
th:value="${_csrf.token}" />