json don't accept in spring boot

Viewed 14

i wanna make easy online shop and i have question. Why json, which send from javasript don't accept in my spring boot application? in the browser I get a 500 error code, I have already tried a lot of things, so I decided to turn here

class

@Data
public class Test {
    private String name;
    private String cost;
}

controller

@Controller
    @RequestMapping("/order")
    @Slf4j
    public class MainController {

        @GetMapping
        public String mainPage(){
            return "main";
        }

        @PostMapping
        public void json (Test test){
            log.info(test.toString());
        }

        @ModelAttribute
        public void addTest(Model model){
            Test test = new Test();
            test.setCost("500");
            test.setName("apple");
            model.addAttribute("test", test);
        }
    }

html template

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
    <script src="https://yastatic.net/jquery/3.3.1/jquery.min.js"></script>

    <meta charset="UTF-8">
    <title></title>
</head>
<body>

    <div  th:object="${test}">
        <h1 th:text="*{name}" id="name"> </h1>
        <h3 th:text="*{cost}" id="cost"> </h3>
        <button type="submit" id="add-button">отправить</button>
    </div>
<script th:src="@{js/main.js}"></script>


</body>
</html>

js script

var name1 = $("#name").text();
var cost1 = $("#cost").text();

$(document).ready(function (){
    $("#add-button").on("click", function (e){
        $.ajax({
            type: "POST",
            url: "/order",
            data: {
                name: name1,
                cost: cost1
            },
            success: function (data){
                alert(data)
            }
        });
    });
});

enter image description here

help me pls, i alreydy tired search answer for my question =(

0 Answers
Related