HTML 表单提交不适用于 Spring Boot 2.3.1

HTML form submit not working with Spring Boot 2.3.1

从 Spring Boot 2.2.7 切换到 Spring Boot 2.3.1

后,针对我的特定案例提交表单时的实体转换不再有效

Category.java

@Entity @Getter @Setter
public class Category implements Serializable {
    private Integer id;
    private String name;
}

SearchForm.java:

@Getter @Setter
public class SearchForm implements Serializable {
    private String q;
    private Category c;
}

HTML形式:

<form method="get" th:action="@{/}" th:object="${searchForm}">
    <input th:field="*{q}" type="text" />
    <select th:field="*{c}">
        <option th:each="cat : ${categories}" th:value="${cat.id}"  th:text="${cat.name}" />
    </select>
</form>

Controller.java:

@PostMapping
public String post( @ModelAttribute final SearchForm searchForm ) {
    // ...
}

以前使用 Spring Boot 2.2.7 表单提交会将“c”从 HTML 表单(select 持有类别 ID)转换为 [=32= 中的类别实体]

切换到 Spring Boot 2.3.1 后,这不再有效。而是在日志中显示错误:

Field error in object 'searchForm' on field 'c': rejected value [424]; codes [typeMismatch.searchForm.c,typeMismatch.c,typeMismatch.com.thevegcat.app.category.Category,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [searchForm.c,c]; arguments []; default message [c]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.thevegcat.app.category.Category' for property 'c'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.thevegcat.app.category.Category' for property 'c': no matching editors or conversion strategy found]]

我认为这是 Spring Data Commons 的错误:

此错误影响 Spring 引导版本 2.3.1、2.2.8 和 2.1.15。

ToEntityConverter 没有按预期工作。


[更新]

此错误已在 2.4.0-M1、2.3.2 和 2.2.9 上修复,但在 2.1.16 上未修复。

参考资料: