Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring Boot 2.0.0M2 Thymeleaf 3未能转换字段_Spring_Jpa_Thymeleaf - Fatal编程技术网

Spring Boot 2.0.0M2 Thymeleaf 3未能转换字段

Spring Boot 2.0.0M2 Thymeleaf 3未能转换字段,spring,jpa,thymeleaf,Spring,Jpa,Thymeleaf,升级到spring-boot-2-m2(thymeleaf 3)后,我发现与JPA关系对应的字段转换失败 无法将值“com.pps2…”的类型[@javax.persistence.ManyToOne@javax.persistence.JoinColumn com.pps2…entities.FormType]转换为类型[java.lang.String]。。。。。FormType@819841af'; 嵌套异常为org.springframework.core.convert.Convert

升级到spring-boot-2-m2(thymeleaf 3)后,我发现与JPA关系对应的字段转换失败

无法将值“com.pps2…”的类型[@javax.persistence.ManyToOne@javax.persistence.JoinColumn com.pps2…entities.FormType]转换为类型[java.lang.String]。。。。。FormType@819841af'; 嵌套异常为org.springframework.core.convert.ConverterNotFoundException:未找到能够从类型[java.util.Optional]转换为类型[java.lang.String]的转换器

JPA实体

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "id_form_type")
private FormType type;

public FormType getType() {
    return type;
}
模板中的代码为:

抛出类似的失败转换错误

当然,当放置直接引用时,它是有效的,但在这种情况下,它会破坏项目中的许多模板。并将
name
生成为
type.id
而不是
type

工作示例


问题-他们为什么更改API?有没有一种方法可以在不重新检查所有模板的情况下解决此问题(例如编写转换器?)

解决方案是编写自己的
可选字符串
转换器。我不知道为什么它被排除在弹簧靴2 M2之外

转换器代码

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.Optional;

@Component
final class OptionalToString implements Converter<Optional<?>, String> {

    public String convert(Optional<?> source) {
        return Objects.toString(source.get(),"");
    }
}
import org.springframework.core.convert.converter.converter;
导入org.springframework.stereotype.Component;
导入java.util.Objects;
导入java.util.Optional;
@组成部分

最后一个类OptionalToString实现了converter当它是Spring时,为什么说“Thymeleaf转换失败”,但有异常?如果是这样,JPA与转换问题有什么关系?aka debug,问题出在这3个软件位上感谢我对Spring环境非常熟悉。我也必须在我的情况下做同样的事情。此外,我认为如果您在回答中也包含了问题中的“工作示例”,这将是非常有帮助的,因为这也是迁移到Spring5+thymeleaf的人可能的修复方法之一