从 Spring Boot 2.2.7 切换到 Spring Boot 2.3.1 后,针对我的特定案例提交表单时的实体转换不再起作用
类别.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 表单(选择持有类别 ID)转换为 SearchForm.java 中的类别实体切换到 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 Boot 版本 2.3.1、2.2.8 和 2.1.15。
ToEntityConverter
没有按预期工作。[更新]
此错误已在 2.4.0-M1、2.3.2 和 2.2.9 上修复,但未在 2.1.16 上修复。
引用:
关于java - HTML 表单提交不适用于 Spring Boot 2.3.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62480677/