Skip to content

How to send PUT/PATCH/DELETE requests in thymeleaf template?

How can I send a DELETE request with thymeleaf? I tried as follows:

<form action="" th:method="delete">
    <input type="submit" class="btn btn-primary" href="/person/123" value="Delete" />
</form>

@RequestMapping(value="/person/{id}", method = RequestMethod.DELETE)
@ResponseBody
public String delete(@PathVariable String id) {
    personService.delete(id);
    return "Successfully deleted";
}

But all that I get is: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported

Answer

If you are using Spring Boot 2.2+, you need to enable support for this explictly. Add the following to application.properties:

spring.mvc.hiddenmethod.filter.enabled=true

Spring Boot < 2.2 always registers the filter, for Spring Boot 2.2 or higher you need to set the property.

Tip: You can replace @RequestMapping(value="/person/{id}", method = RequestMethod.DELETE) with @DeleteMapping("/person/{id}")