I am trying to create an account controller using Spring Boot. I have an html file for the login page located under static/login.html. The page loads perfectly fine when I do not map a POST request to the same path.
I have an AccountController class:
@RestController
public class AccountController {
@RequestMapping(value = "/login", method = RequestMethod.POST)
public Account login(@RequestBody Map<String, Object> body) {
// code
}
}
This controller disables the GET request to the html page. When trying to access the page, I receive;
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported
So my question is essentially, how do I get both the POST and GET request working at the same time. If there is a better file structure I can use for the static content, please suggest it.
This should work
@RequestMapping(value = "/login", method = { RequestMethod.GET, RequestMethod.POST })
public Account login(@RequestBody Map<String, Object> body) {
// code
}
This way, the controller mapping will be available for both methods
UPDATE
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String serveLogin(... (if needed) ) {
// code to serve your static content
return "index.html";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public Account login(@RequestBody Map<String, Object> body) {
// code to handle your login form POST submit
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.