Spring 无法转换类型为';java.lang.String';至所需类型';java.time.LocalDate';对于不动产';日期';百里香

Spring 无法转换类型为';java.lang.String';至所需类型';java.time.LocalDate';对于不动产';日期';百里香,spring,kotlin,thymeleaf,java-time,Spring,Kotlin,Thymeleaf,Java Time,简单pojo类 @PostMapping("/hi") fun testum(@ModelAttribute datum: Datum) { println(datum) } 我试图以表单形式发送日期,但出现以下异常: class Datum( @DateTimeFormat(pattern = "yyyy-MM-dd") var date: LocalDate? = null ) 但如果我将类型从LocalDate更改为String,它就可以正常工作

简单pojo类

@PostMapping("/hi")
fun testum(@ModelAttribute datum: Datum) {
    println(datum)
}
我试图以表单形式发送日期,但出现以下异常:

class Datum(
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        var date: LocalDate? = null
)
但如果我将类型从LocalDate更改为String,它就可以正常工作。我想映射date类的form-to-date属性中的日期。有人能帮我吗?有链接吗?谢谢


此链接对我没有帮助

我只是使用此作为控制器创建了您的示例:

Resolved exception caused by Handler execution: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'datum' on field 'date': rejected value [2018-06-20]; codes [typeMismatch.datum.date,typeMismatch.date,typeMismatch.java.time.LocalDate,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [datum.date,date]; arguments []; default message [date]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDate' for property 'date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.LocalDate] for value '2018-06-20'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2018-06-20]]
这是一种观点:

@Controller
public class StackOverflow {

@GetMapping("/stack")
public String template(Model m) {
    m.addAttribute("datum", new Datanum());
    return "stackoverflow.html";
}

@PostMapping("/stack2")
public String testum(@ModelAttribute Datanum user) {
    System.out.println(user.getDate());
    return null;
}
}
它成功了:

我看到的区别是,您在Bean上使用var

   import java.time.LocalDate;

   import org.springframework.format.annotation.DateTimeFormat;

   public class Datanum{
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        private LocalDate date;

        public LocalDate getDate() {
            return date;
        }

        public void setDate(LocalDate date) {
            this.date = date;
        }
    }
我认为这就是Java10,不是吗?你为什么不试着用它呢

就像我做的豆子,也许能帮你

使用LocalDate代替var


希望这行得通这是个老问题,但我只给未来的读者留下一个答案。
这不是thymeleaf的问题,更多的是绑定的问题。
@DateTimeFormat
如果
基准
类更改如下,则上述问题中的示例将起作用:

解决方案1。 请注意,
date
字段已从主构造函数中的声明移动到类主体中。
(注意从
Datum(…)
Datum{…}
的更改)

解决方案2。 如果由于将其作为
数据类
或其他原因而需要在构造函数中声明,则必须使用注释。
但是,请注意解决方案2可能并不总是有效。我无法在示例项目中复制,但在实际项目中,出现了一个问题,
@field:DateTimeFormat
没有将请求参数字符串正确绑定到
日期
对象。它有时有效,有时无效,这使得调试非常棘手。
当它不起作用时,它会抛出错误,比如object='Datum'的
验证失败。错误计数:1
,而恢复到解决方案1始终有效。每次我们再次编译时,解决方案2有时会正常工作,并在没有任何代码更改的情况下随机中断。

我们严格地将
@DateTimeFormat
注释字段放在类主体声明中,以确保其工作。

它不是java 10,而是kotlin。谢谢从kotlinclass数据(@DateTimeFormat(pattern=“yyyy-MM-dd”)var-date:LocalDate)切换回java的问题是,我在主构造函数中创建属性,我不知道为什么,但thymeleaf不能以这种方式设置值。即使是类数据(@DateTimeFormat(pattern=“yyyy-MM-dd”)var-date:LocalDate=LocalDate.MIN),它也不能使用默认值。不幸的是,这个答案与问题完全无关。它没有使用正确的语言(kotlin),它指出的只是它在Java中工作。原始问题中的主要问题是没有正确设置注释使用目标,因此此答案可能会被否决,以降低可见性,防止将来的读者感到困惑。@Yussef You save my day
<!DOCTYPE html>
<html xmlns:th="http://www.thymleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form th:action="@{/stack2}"  th:object="${datum}" method="post">
            <p>date : <input type="date" th:field="*{date}"/> </p>
            <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
        </form>
</body>
</html>
   import java.time.LocalDate;

   import org.springframework.format.annotation.DateTimeFormat;

   public class Datanum{
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        private LocalDate date;

        public LocalDate getDate() {
            return date;
        }

        public void setDate(LocalDate date) {
            this.date = date;
        }
    }
  var date: LocalDate? = null
class Datum {
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    var date: LocalDate? = null
}
class Datum (
   @field:DateTimeFormat(pattern = "yyyy-MM-dd") var date: LocalDate? = null
)