我正在尝试设置 Spring Data/JPA DomainClassConverter 以自动将(字符串)id 转换为域类本身。
我的项目是使用 Java Config(所以没有 xml)。
在我的 WebConfig 中,我目前有:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new DomainClassConverter<DefaultFormattingConversionService>((DefaultFormattingConversionService) registry));
}
}
这似乎成功连接了 DomainClassConverter,因为我可以在打印时在转换服务中看到它:
ConversionService converters =
..<default converters>..
org.springframework.data.repository.support.DomainClassConverter@6ea4ce0d, org.springframework.core.convert.support.IdToEntityConverter@5d3f03b, org.springframework.core.convert.support.ObjectToObjectConverter@1d40b47a
但是当提交嵌套表单(带有客户参考的订单)时,客户不会自动转换,因此我得到:
Failed to convert property value of type java.lang.String to required type org.mycomp.domain.Customer for property customer; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.mycomp.domain.Customer] for property customer: no matching editors or conversion strategy found
我想知道我是否在这里做错了什么?