I'm trying to access JPA Data with REST using spring-boot-starter-data-rest. I want to use a different method than the CrudRepository has. But the framework responds with the following exception:
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]
Endpoint
http://localhost:8080/rest/cities/search/findByCountry?name=Brazil
CityRepository.java
@RepositoryRestResource(collectionResourceRel = "cities", path = "cities")
public interface CityRepository extends CrudRepository<City, Long> {
List<City> findByCountry(@Param("name") Country country);
}
City.java
@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;
}
Country.java
@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;
}
When I call http://localhost:8080/rest/cities/ , I get the list of cities normally. I setted the configureRepositoryRestConfiguration to config.setBasePath("/rest");
...findByCountry?name={name:"Brazil"}
? Looks like it expects json value – StanislavL