Java QueryMethodParameterConversionException
我正在尝试使用SpringBootStarterDataREST使用REST访问JPA数据。 我想用一种与Crudepository不同的方法。但框架的响应有以下例外:Java QueryMethodParameterConversionException,java,spring,spring-data,spring-data-jpa,spring-data-rest,Java,Spring,Spring Data,Spring Data Jpa,Spring Data Rest,我正在尝试使用SpringBootStarterDataREST使用REST访问JPA数据。 我想用一种与Crudepository不同的方法。但框架的响应有以下例外: exception is org.springframework.data.repository.support.QueryMethodParameterConversionException: Failed to convert Brazil into hello.Country!] with root cause org.s
exception is org.springframework.data.repository.support.QueryMethodParameterConversionException: Failed to convert Brazil into hello.Country!] with root cause
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [@org.springframework.data.repository.query.Param hello.Country]
终点
CityRepository.java
@RepositoryRestResource(collectionResourceRel = "cities", path = "cities")
public interface CityRepository extends CrudRepository<City, Long> {
List<City> findByCountry(@Param("name") Country country);
}
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Entity
@Table(name = "City")
public class City implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@JsonInclude(value=Include.ALWAYS)
private long id;
@NotNull
@Column(name="name")
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "country_id")
@Embedded
private Country country;
protected City() {}
public City(long id, String nome) {
this.id = id;
this.name = nome;
}
public Country getCountry() {
return country;
}
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Entity
public class Country implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@NotNull
@Column(name = "name")
private String name;
@OneToMany(fetch = FetchType.LAZY, mappedBy="country")
private Set<City> cities;
protected Country() {}
public Country(long id, String nome) {
this.id = id;
this.name = nome;
}
Country.java
@RepositoryRestResource(collectionResourceRel = "cities", path = "cities")
public interface CityRepository extends CrudRepository<City, Long> {
List<City> findByCountry(@Param("name") Country country);
}
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Entity
@Table(name = "City")
public class City implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@JsonInclude(value=Include.ALWAYS)
private long id;
@NotNull
@Column(name="name")
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "country_id")
@Embedded
private Country country;
protected City() {}
public City(long id, String nome) {
this.id = id;
this.name = nome;
}
public Country getCountry() {
return country;
}
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Entity
public class Country implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@NotNull
@Column(name = "name")
private String name;
@OneToMany(fetch = FetchType.LAZY, mappedBy="country")
private Set<City> cities;
protected Country() {}
public Country(long id, String nome) {
this.id = id;
this.name = nome;
}
@JsonIgnoreProperties({“hibernateLazyInitializer”,“handler”})
@实体
公共类国家/地区实现可序列化{
私有静态最终长serialVersionUID=1L;
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人长id;
@NotNull
@列(name=“name”)
私有字符串名称;
@OneToMany(fetch=FetchType.LAZY,mappedBy=“country”)
私人城市;
受保护国家({}
公共国家/地区(长id,字符串名称){
this.id=id;
this.name=nome;
}
当我调用时,我通常会得到城市列表。我将configureRepositoryRestConfiguration设置为config.setBasePath(“/rest”)
根据文件规定,我们需要:
字符串创建对象,如
字符串创建对象,如
我使用nativeQuery执行了以下操作来解决此问题:
@Query(value = "SELECT * FROM City c JOIN Country co
ON c.country_id = co.id WHERE co.name
LIKE (CONCAT_WS('%',:name, '%'))", nativeQuery = true)
List<City> country(@Param("name") String name);
@Query(value=“SELECT*FROM City c JOIN Country co
在c.country_id=co.id上,其中co.name
比如(CONCAT_WS(“%”,:name“%”)“,nativeQuery=true)
列表国家(@Param(“name”)字符串名称);
我使用nativeQuery执行了以下操作来解决此问题:
@Query(value = "SELECT * FROM City c JOIN Country co
ON c.country_id = co.id WHERE co.name
LIKE (CONCAT_WS('%',:name, '%'))", nativeQuery = true)
List<City> country(@Param("name") String name);
@Query(value=“SELECT*FROM City c JOIN Country co
在c.country_id=co.id上,其中co.name
比如(CONCAT_WS(“%”,:name“%”)“,nativeQuery=true)
列表国家(@Param(“name”)字符串名称);
调用…findByCountry?name={name:“Brazil”}
?看起来它期望json valueCountry不是字符串,而且我没有看到设置了转换器。研究一下这些。你也可以从这里开始:这个问题有一个简单转换器的好例子:调用…findByCountry?name={name:“Brazil”}
?看起来它希望json valueCountry不是一个字符串,而且我没有看到设置了转换器。研究这些。你也可以从这里开始:这个问题有一个简单转换器的好例子: