"Request method 'POST' not supported" when I try to save Person due to <form method="POST">
Controller methods where can be mistaken:
Here I go to URL "/create" with <form method="POST>
@GetMapping("/create")
public ModelAndView create() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/create.jsp");
return modelAndView;
}
This is create.jsp
<%@ page isELIgnored="false"%>
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Create</title>
</head>
<body>
<h1>Create author</h1>
<form method="POST" action="">
Id <input name="userId"> <br>
Last name: <input name="lastName"> <br>
First name: <input name="firstName"> <br>
Second name: <input name="secondName"> <br>
Phone: <input name="phone"> <br>
Hobby: <input name="hobby"> <br>
BitBucketUrl: <input name="bitBucketUrl"> <br>
<input type="submit" value="Create">
</form>
</body>
</html>
And finally @Postmapping method
@PostMapping
public ModelAndView createPerson(
@RequestParam("id") String id,
@RequestParam("lastName") String lastName,
@RequestParam("firstName") String firstName,
@RequestParam("secondName") String secondName,
@RequestParam("phone") String phone,
@RequestParam("hobby") String hobby,
@RequestParam("bitBucketUrl") String bitBucketUrl) {
personCache.create(Person.builder()
.setId(id)
.setFirstName(firstName)
.setLastName(lastName)
.setSecondName(secondName)
.setPhone(phone)
.setHobby(hobby)
.setBitBucketUrl(bitBucketUrl)
.build());
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("id", id);
modelAndView.addObject("lastName", lastName);
modelAndView.addObject("firstName", firstName);
modelAndView.addObject("secondName", secondName);
modelAndView.addObject("phone", phone);
modelAndView.addObject("hobby", hobby);
modelAndView.addObject("bitBucketUrl", bitBucketUrl);
modelAndView.setViewName("/all.jsp");
return modelAndView;
}
Page all.jsp is working (realized in method @GetMapping("/all")).
I want, when the button "create" is pushed, go to page "/all", where I can see a new Person. But I take an error "Request method 'POST' not supported" in the terminal, and error "type=Method Not Allowed, status=405" in the browser.
Also I create default page index.jsp, and try modelAndView.setViewName("/index.jsp") in @PostMapping method, but unfortunately, even in this case, when I push "create" browser don't go to index.jsp
Solution 1:
By default browser does get-requests only, so create another controller method with get-mapping and add person object to the model so that you can set values in the view page and when you press submit give post-mapping so that you can assign values that are entered in view page to the person object.
Solution 2:
Browser address bar sends GET requests. You can use smth like Postman to send requests which you need.