T
T
TonyJa2018-08-31 06:23:33
Java
TonyJa, 2018-08-31 06:23:33

Why isn't MediaType supported in the controller?

Good afternoon.
There is such an endpoint

@PostMapping(value = "/products/add", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public String addProduct(@RequestBody ProductDTO productDTO) {
        service.addProduct(productDTO);
        return "redirect:/products";
    }

And such a jps
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Add product</title>
</head>
<body>
<div>
    <form action="#" th:action="@{/products/add}" th:object="${productDTO}" method="post">
        <input type="hidden" name="id">
        <input type="text" name="productName" placeholder="Press name" />
        <input type="text" name="productDescription" placeholder="Press description">
        <input type="number" name="productPrice" placeholder="Press price">
        <input type="number" name="stockBalance" placeholder="Press stockbalance">
        <button type="submit">Add product</button>
    </form>
</div>
</body>
</html>

When I send a POST from this form, I get a 415 response.
Resolved exception caused by Handler execution: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

Tell me what could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Greben86, 2018-08-31
@Greben86

Good afternoon. Try explicitly specifying the Content-Type for the form:

<form action="#" th:action="@{/products/add}" th:object="${productDTO}" method="post"  enctype="multipart/form-data">

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question