How to use two ContentTypes in one JSP(web app on Spring MVC): text/html && application/json

66 Views Asked by At

I'm developing a simple crud app without frontend, but I want to have a very simple visual for myself and the viewers. Now I have created a simple jsp-file with a set of tags 'a'. It was not difficult to complete all GET requests. However, POST requests give an error "HTTP Status 415 – Unsupported Media Type".

This is my .jsp

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
    <title>Title</title>
    <style>
      
    </style>
</head>

<body>

<div class="admin div">
    <h2>Options for Admin</h2>
    <li><a href="">Create User</a></li>
    <li><a href="">Update User</a></li>
    <li><a href="">Delete User</a></li>
    <li><a href="${pageContext.request.contextPath}/rest/admin/users/100003" class="deal">Get User by ID(for example 100003</a></li>
    <li><a href="${pageContext.request.contextPath}/rest/admin/users/[email protected]" class="deal">Get User by Email(for example - [email protected])</a></li>
    <li><a href="${pageContext.request.contextPath}/rest/admin/users" class="deal">All users</a></li>
    <br>
    <li><a href="">Create restaurant</a></li>
    <li><a href="">Update restaurant</a></li>
    <li><a href="">Delete restaurant</a></li>
    <br>
    <li>
    <form  method="post" action="/rest/admin/restaurants/100005/meals">
            <div class="add">
                <dt>Description</dt>
                <input type="text" name="description" value="Some Meal"><br> </input>
            </div>
            <div class="add">
                <dt>Price</dt>
                <input type="number" name="price" value="50"><br> </input>
            </div>
            <button type="submit">SAVE</button>
    </form>
    </li>
    <br>
    <li><a href="">Update meal</a></li>
    <li><a href="">Delete meal</a></li>
    <li><a href="/rest/admin/restaurants/100005/meals" class="deal">Get actual meals (for example - by restaurant with ID = 100005)</a></li>
    <li><a href="/rest/admin/restaurants/100005/meals/100010" class="deal">Get meal by ID (for example - by restaurant with ID = 100005, meal ID = 100010)</a></li>
</div>

</body>
</html>

When i try to send POST-request I get 'HTTP Status 415 – Unsupported Media Type'.

In network:

The response must have a 'json/application' content type, but the request has 'text/html'. It doesn't work just to add another contentType in jsp-file.

Please, share your experiance.

1

There are 1 best solutions below

0
Reddi Kalyan On

Try to make your @PostMapping controller method mapping looks like this:

@PostMapping(value = "/yourUrl", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_HTML_VALUE}, produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_HTML_VALUE})