将对象从thymeleaf模板发送到Rest Controller会返回“不支持的媒体类型”

时间:2019-01-06 20:23:57

标签: spring utf-8 thymeleaf

我正在尝试使用百里香将一些对象字段发布到RestController上。

但是帖子的结果返回看起来像是一个解析错误:

  

发生意外错误(类型=不支持的介质类型,状态= 415)。内容类型   'application / x-www-form-urlencoded; charset = UTF-8'不支持

索引页将两个属性发送到控制器,然后控制器调用构建新对象的业务服务。

 <!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">

<head>

<meta charset="utf-8">
<meta name="viewport"
    content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">

<title>Simple Sidebar - Start Bootstrap Template</title>

<!-- Bootstrap core CSS -->
<link th:href="@{/index/vendor/bootstrap/css/bootstrap.min.css}"
    rel="stylesheet">

<!-- Custom styles for this template -->
<link th:href="@{/index/css/simple-sidebar.css}" rel="stylesheet">

</head>

<body>

        <!-- Page Content -->
        <div id="page-content-wrapper">
            <div class="container-fluid">
                <form action="#" th:action="@{/accounts}" th:object="${account}" method="post" enctype="application/json">
                    <div class="form-row">
                        <div class="form-group col-md-4 ">
                            <label for="inputState">Departement</label>
                            <input type="text" th:field="*{owner}"  class="form-control" placeholder="Type in the department ..." />
                            </div>
                    </div>  
                    <div class="form-row">
                        <div class="form-group col-md-4 ">
                            <label for="inputState">Budget</label>
                            <input type="number" step="0.01" th:field="*{budget}"  class="form-control" placeholder="Type in Budget ..." />
                            </div>
                    </div>
                    <button class="btn btn-primary" type="submit">Enregistrer</button>
                </form>
            </div>
        </div>
        <!-- /#page-content-wrapper -->

</body>
</html>

这是Rest Controller:

@RestController
public class AccountController {

@Autowired
private AccountService accountService;

@RequestMapping(value="/accounts", method=RequestMethod.POST)
public void addAccount(@RequestBody Account account ) {
    accountService.addAccount(account);
}

}

帐户是一个简单的POJO:

public class Account {


private String id;
private String owner;
private double budget;
private double budgetInvest;
private double budgetFonction;

public Account() {

}

public Account(String id,String owner, double budget, double budgetInvest, double budgetFonction 
        ) {
    super();
    this.id = id;
    this.owner=owner;
    this.budget = budget;
    this.budgetInvest = budgetInvest;
    this.budgetFonction = budgetFonction;
}

public Account (String owner, double budget) {
    this.owner = owner;
    this.budget=budget;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public double getBudget() {
    return budget;
}

public void setBudget(double budget) {
    this.budget = budget;
}

public double getBudgetInvest() {
    return budgetInvest;
}

public void setBudgetInvest(double budgetInvest) {
    this.budgetInvest = budgetInvest;
}

public double getBudgetFonction() {
    return budgetFonction;
}

public void setBudgetFonction(double budgetFonction) {
    this.budgetFonction = budgetFonction;
}

public String getOwner() {
    return owner;
}

public void setOwner(String owner) {
    this.owner = owner;
}




}

add方法只是将对象添加到对象列表中。

我在这里做错了什么?

2 个答案:

答案 0 :(得分:0)

我认为您的控制器存在问题。首先,它不会是@RestController。将会是@Controller。其次,它不会是@RequestBody。将会是@ModelAttribute。因此,请像这样编写控制器

@Controller
public class AccountController {

@Autowired
private AccountService accountService;

@RequestMapping(value="/accounts", method=RequestMethod.POST)
public void addAccount(@ModelAttribute("account") Account account ) {
   System.out.ptintln("Checking");
    accountService.addAccount(account);
}

The most relevant to the discussion of REST, the @RestController annota- tion tells Spring that all handler methods in the controller should have their return value written directly to the body of the response, rather than being carried in the model to a view for rendering.这本书spring in action的这一行。因此,在这里您应该使用@Controller批注。

答案 1 :(得分:-1)

您可以使用restcontroller,但是您必须使用ajax post来运行“ / account”

Cmiiw