如何解决找不到能够从[java.lang.String]类型转换为[@Autowired @ManyToOne @JoinColumn com.papertrue.country.Country]类型的转换器
因此,我对JPA还是很陌生,在将一个对象映射到另一个对象时遇到了麻烦。这是我的用户实体:
package com.papertrue.user;
import com.papertrue.country.Country;
import com.papertrue.currency.Currency;
@Entity
@ConfigurationProperties("user")
@Table(name = "users")
public class User {
@Id
@Column(name = "id")
private String userId;
private String name;
@Column(name = "phone_ext")
private String phoneExt;
@Column(name = "phone_no")
private String phoneNo;
private String email;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "country")
private Country country;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "currency")
private Currency currency;
@Column(name = "fb_id")
private String facebookId;
@Column(name = "google_id")
private String googleId;
@Column(name = "current_balance")
private int currentBalance;
@Column(name = "is_enabled")
private boolean isEnabled;
@Column(name = "free_sample_used")
private boolean isfreeSampleUsed;
@Column(name = "client_id")
private String clientId;
}
我的国家实体:
package com.papertrue.country;
import com.papertrue.currency.Currency;
@Entity
@ConfigurationProperties("country")
@Table(name = "countries")
public class Country {
@Id
private String code;
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "currency")
private Currency currency;
@Column(name = "isd_ext")
private String ISDExt;
@Enumerated(EnumType.STRING)
private transient Region region;
}
还有我的货币实体:
package com.papertrue.currency;
@Entity
@ConfigurationProperties("currency")
@Table(name = "currencies")
public class Currency {
@Id
private String code;
private String symbol;
private String name;
private int multiplier;
@Column(name = "minVal")
private int minValue;
}
因此,当我在Country对象中映射Currency with-时,它是有效的,但是当我对User和Country执行相同的操作时,它却以某种方式不起作用。我得到org.springframework.core.convert.ConverterNotFoundException
。这是错误:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132) ~[spring-test-5.3.0.jar:5.3.0]
Caused by: org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'user-com.papertrue.user.User': Could not bind properties to 'User' : prefix=user,ignoreInvalidFields=false,ignoreUnkNownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'user.country' to com.papertrue.country.Country
... 70 common frames omitted
Caused by: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'user.country' to com.papertrue.country.Country
... 89 common frames omitted
Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [@javax.persistence.ManyToOne @javax.persistence.JoinColumn com.papertrue.country.Country]
... 105 common frames omitted
这是在我运行测试时发生的:
package com.papertrue;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBoottest;
import com.papertrue.exceptions.PaperTrueUserNotFoundException;
import com.papertrue.user.UserService;
@SpringBoottest
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void contextLoads() {
// Testing getEmailInstance
System.out.println("====== getEmailInstance() ======");
try {
System.out.println(userService.getEmailInstance("text@papertrue.com").getName());
} catch (PaperTrueUserNotFoundException e) {
e.printstacktrace();
}
}
@SpringBootApplication
static class TestConfiguration {
}
}
UserService服务:
package com.papertrue.user;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import com.papertrue.exceptions.PaperTrueUserNotFoundException;
@Service
@EnableConfigurationProperties(User.class)
public class UserService {
@Autowired
private UserRepository userRepository;
/**
* Returns user whose email matches to parameter email's value.
*
* @param email
* @return
* @throws PaperTrueUserNotFoundException
*/
public User getEmailInstance(String email) throws PaperTrueUserNotFoundException {
Optional<User> optional = userRepository.findByEmail(email);
if (optional.isPresent()) {
return optional.get();
}
throw new PaperTrueUserNotFoundException("User Not Found");
}
}
以及供参考的存储库:
package com.papertrue.user;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User,String> {
Optional<User> findByEmail(String email);
}
解决方法
该异常告诉您,它无法按照@ConfigurationProperties
的建议设置配置属性。因为对于国家/地区,它具有一个String
值(可能在application.properties
中),但是需要一个Country
,并且没有用于此的转换器。
这带来了一个问题:“您尝试使用@ConfigurationProperties
注释实现什么?”
在JPA实体上使用它至少是非传统的,因为bean和实体通常是两个非常不同的东西。
因此您应该删除@ConfigurationProperties
批注
或按以下说明注册自定义转换器:
https://www.logicbig.com/tutorials/spring-framework/spring-boot/custom-configuration-properties-binding.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。