使用 Thymeleaf 和 Spring boot 以 JSON 格式发送表单

tab*_*bim 6 thymeleaf spring-boot

我正在使用 Thymeleaf 的表单将数据发送到我的服务。

这是我的表格:

<form th:object="${individualEntity}" th:action="@{/save}" method="post">
<div class="form-group">
    <label>FirstName:</label>
        <input type="text" class="form-control" th:field="*{individualName.givenName}"/>
</div>
<div class="form-group">
    <label>LastName:</label>
        <input type="text" class="form-control" th:field="*{individualName.surname}"/>
</div>
<div class="form-group">
    <label>Address:</label>
    <input type="text" th:field="*{address}"/>
</div>

<div class="row">
    <button type="submit">Submit</button>
</div>
Run Code Online (Sandbox Code Playgroud)

当我调试使用此表单发送的请求时,它会提供以下数据: individualName.givenName=xxxx&individualName.surname=xxxxx&address=xxxx

不过,我希望发送时采用 JSON 格式,因为我的微服务使用 JSON。

我应该怎么做才能纠正这个问题!

这是我的服务:

    @PostMapping(value = "save")
public String saveIndividual(Individual individual){
    individualService.saveIndividual(individual);
    return "redirect:/getIndividual/" + individual.getId();
}
Run Code Online (Sandbox Code Playgroud)

谢谢