Search code examples
springutf-8thymeleaf

Sending object from thymeleaf template to Rest Controller returns "Unsupported Media Type"


I'm trying to POST some object fields to a RestController using thymeleaf.

But the result of the post returns what looks like a parsing error :

There was an unexpected error (type=Unsupported Media Type, status=415). Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

The index page sends two attributes to the controller which then calls the business service that builds the new object.

 <!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>

This is the 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);
}

}

Account is a simple 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;
}




}

And the add method simply adds the object to a list of objects.

What am i doing wrong here ?


Solution

  • I think problem in you controller. First it won't be @RestController. It will be @Controller. Second it won't be @RequestBody. It will be @ModelAttribute.So write your controller like

    @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. This line from book spring in action. So here you should use @Controller annotation.