问题
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
@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:
<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 ) {
// ...
}
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
After switching to Spring Boot 2.3.1 this is not working anymore. An error is displayed in log instead:
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]]
回答1:
I think it's a bug of Spring Data Commons:
- Translation of URI segments and request parameters into aggregates broken
This bug affects Spring Boot version 2.3.1, 2.2.8 and 2.1.15.
ToEntityConverter does not work as expected.
[update]
This bug has been fixed on 2.4.0-M1, 2.3.2 and 2.2.9, but not been fixed on 2.1.16.
refs:
- https://docs.spring.io/spring-data/commons/docs/current/changelog.txt
来源:https://stackoverflow.com/questions/62480677/html-form-submit-not-working-with-spring-boot-2-3-1