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

问题描述:

从Spring Boot 2.2.7切换到Spring Boot 2.3.1后,针对我的具体情况,在提交表单时进行实体转换不再起作用

Entity conversion upon form submit for my specific case is not working anymore after switching from Spring Boot 2.2.7 to Spring Boot 2.3.1

Category.java

Category.java

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

SearchForm.java:

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:

Controller.java:

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

以前,在Spring Boot 2.2.7中,表单commit会将"c"转换为"c".从HTML表单(选择保留类别的ID)到SearchForm.java中的类别实体

Previously with Spring Boot 2.2.7 form submit would convert "c" from HTML form (select is holding ID of Category) to Category entity in SearchForm.java

切换到Spring Boot 2.3.1之后,它不再起作用.而是在日志中显示错误:

After switching to Spring Boot 2.3.1 this is not working anymore. An error is displayed in log instead:

字段"c"上的对象"searchForm"中的字段错误:拒绝的值[424];代码[typeMismatch.searchForm.c,typeMismatch.c,typeMismatch.com.thevegcat.app.category.Category,typeMismatch];参数[org.springframework.context.support.DefaultMessageSourceResolvable:代码[searchForm.c,c];参数[];默认消息[c]];默认消息[未能将类型'java.lang.String'的属性值转换为属性'c'的必需类型'com.thevegcat.app.category.Category';嵌套的异常为java.lang.IllegalStateException:无法将属性"c"的类型"java.lang.String"的值转换为所需的类型"com.thevegcat.app.category.Category":未找到匹配的编辑器或转换策略]]

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的错误:

I think it's a bug of Spring Data Commons:

此错误会影响Spring Boot版本2.3.1、2.2.8和2.1.15.

This bug affects Spring Boot version 2.3.1, 2.2.8 and 2.1.15.

ToEntityConverter does not work as expected.