There is a Spring project with a single POST request and it doesn't work as it should.
Сontroller:
@Controller
public class RecipeController {
@Autowired
private RecipeRepository recipeRepository;
@GetMapping("/recipes")
public String recipes(Model model) {
Iterable<Recipe> recipes = recipeRepository.findAll();
model.addAttribute("recipes", recipes);
return "recipes";
}
@GetMapping("/recipes/new-recipe")
public String createRecipe(Model model) {
return "new-recipe";
}
@PostMapping("/recipe/new-recipe")
public String addNewRecipe(@RequestParam String title, @RequestParam String description, @RequestParam String ingredient, @RequestParam String fullText, Model model) {
Recipe recipe = new Recipe(title, description, ingredient, fullText);
recipeRepository.save(recipe);
return "redirect:/recipes";
}
}
When making a POST request through this form:
<form action="/recipes/new-recipe/add" method="post">
<div >
<label for="title">Якою буде назва вашої страви:</label>
<input type="text" id="title" name = "title" placeholder="Введіть назву">
</div>
<div >
<label for="description">Опишіть вашу страву:</label>
<textarea id="description" name = "description" rows="2"></textarea>
</div>
<div >
<label for="ingredients">Які інгрідієнти ви використаєте:</label>
<textarea id="ingredients" name = "ingredients" rows="4"></textarea>
</div>
<div >
<label for="recipe">Рецепт страви:</label>
<textarea id="recipe" name = "fullText" rows="6"></textarea>
</div>
<div >
<button type="submit" >Створити рецепт</button>
</div>
</form>
I am getting this problem:
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Sun Apr 17 04:56:48 EEST 2022 There was an unexpected error (type=Method Not Allowed, status=405). Request method 'POST' not supported org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:253) at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:442) at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:383) at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.getHandlerInternal(RequestMappingInfoHandlerMapping.java:125) at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.getHandlerInternal(RequestMappingInfoHandlerMapping.java:67) at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:498) at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1261) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1043) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) at javax.servlet.http.HttpServlet.service(HttpServlet.java:681) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:889) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1743) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748)
And the thing is that I was thinking that let's say the request blocks csrf, but when I added it to the form in the form:
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
All the same error, then I tried to make a POST request via curl:
curl --request POST http://localhost:8081/recipe/new-recipe -d "title=title&description=decri&ingredient=tomate&fullText=cook"
And I succeeded and the record was added to the database.
I have already tried a lot of things and the method itself changed the parameters, removed bootstrap from the markup, nothing helped. I can assume the problem is in the HTML, but I did not find anything in this regard. But I noticed that the problem is when I work through the form. With the request and its processing, everything seems to be normal.
CodePudding user response:
Check if it is a mistype, your annotation is /recipe/new-recipe and your form is /recipes/new-recipe/add with add. They should be equal.
@PostMapping("/recipe/new-recipe")
<form action="/recipes/new-recipe/add" method="post">
CodePudding user response:
In addition to @Shura16's answer, please change
name = "ingredients"
to
name = "ingredient"