0

I've got this error after I submit following form:

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

Here is Thymeleaf form html tag:

<form th:method="put" action="/orders/3" th:object="${order}">
    ...
</form>

And the controller:

@PutMapping("/{id}")
public String update(@PathVariable("id") Long id, 
        @ModelAttribute(name = "order") OrderDto order) {
    ...
    return "redirect:/orders";
}

When I change @PutMapping("/{id}") to @PostMapping("/{id}") the error fix but why it's not recognize controller related method with @PutMapping annotation?

UPDATE:

This link spring+ thymeleaf unable to update does not fix my problem, because I'm using th:method not method property and then it's generated html containing POST method with hidden input with PUT value. If I have to use @PostMapping annotation I want to know @PutMapping usage.

Mehdi Rahimi
  • 1,453
  • 5
  • 20
  • 31

1 Answers1

2

HTML does not support PUT or DELETE HTTP methods for its method attribute.

When you use th:method="PUT" thymeleaf will create hidden input as below screenshot and changes method value to POST.

Because of this change, @PutMapping does not work by default, but if you do @PostMaping it will.

enter image description here

If you want to use it with @PutMapping:

You can enable this by adding spring.mvc.hiddenmethod.filter.enabled=true to your application.properties file. See Spring Boot how to use HiddenHttpMethodFilter

İsmail Y.
  • 3,579
  • 5
  • 21
  • 29