Spring Boot + LocalDate: "No primary or default constructor"

Stefan Fischer :

I have created a simple form with an <input type="date" ...> for a Spring Boot application. I'd like that turned into a LocalDate in the controller. I get the error:

Fri Aug 17 15:32:01 CEST 2018
There was an unexpected error (type=Internal Server Error, status=500).
No primary or default constructor found for class java.time.LocalDate

That is certainly correct, but - as far as I understand - should not matter, because, according to the documentation: "[...] the @DateTimeFormat annotation should work with java.time on Java 8 and Spring 4."

Controller method:

@PostMapping("/users")
public String register(String user, String pw, @DateTimeFormat(iso=DateTimeFormat.ISO.DATE) LocalDate beginning){

    MyUser myUser = MyUser.create(user,pw);
    MyUser.setEnabled(beginning);

    repo.register(myUser);

    return "redirect:/users/"+user;
}

html/thymeleaf:

<form method="POST">
<div><label> Benutzername : <input type="text" name="user"/> </label></div>
<div><label> Passwort: <input type="password" name="password"/> </label></div>
<div><label> Freigeschaltet ab (optional): <input type="date" name="beginning"/> </label></div>
<div><input type="submit" value="Registrieren"/></div>
</form>

pom (excerpt)

 <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.4.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
...
</dependencies>   

<properties>
    <java.version>1.8</java.version>
</properties>

Edit: Complete html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
  xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Users</title>
</head>
<body>

<div th:insert="fragments :: logoutButton">...</div>

<div class ="user_list">
<h2>Registrierte Nutzer</h2>

<div>
<table border="1">
    <tr>
        <td>Name</td>
        <td>Freigeschaltet ab</td>
        <td>Freigeschaltet bis</td>
        <td>Passwort gesetzt</td>
        <td>Freigaben</td>
    </tr>
    <tr th:each="user : ${users}">
        <td th:text="${user.userName}"/>
        <td th:text="${user.enabled}"/>
        <td th:text="${user.disabled}"/>
        <td th:text="${user.pwSet}"/>
        <td th:text="${user.authorities}"/>
    </tr>
</table>
</div>
</div>

<div class ="register_user">
<h2>Neuen Benutzer Registrieren</h2>

<form method="POST">
<div><label> Benutzername : <input type="text" name="user"/> </label>    </div>
<div><label> Password: <input type="password" name="password"/> </label></div>
<div><label> Freigeschaltet ab (optional): <input type="date" name="beginning"/> </label></div>
<div><input type="submit" value="Registrieren"/></div>
</form>
</div>
</body>
</html>
Nikolas :

The LocaDate doesn't provide own constructor but is instantiated using the static method call.

This has been resolved in SPR-13362 and works as expected. The workaround is to create a model attribute using the @ModelAttribute annotation and passing it to the request:

@ModelAttribute
LocalDate initLocalDate() {
    return LocalDate.now();
}

And pass it then to the request:

@PostMapping("/users")
public String register(
    String user, 
    String pw,  
    @DateTimeFormat(iso=DateTimeFormat.ISO.DATE)  @ModelAttribute LocalDate beginning) 
{
    // ...
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=108339&siteId=1