1
votes

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");

2
What aboutn the call ...findByCountry?name={name:"Brazil"}? Looks like it expects json valueStanislavL
Country isn't a String, and I don't see a converter set up. Research those. You could also start here: this question has a decent example of a simple converter: stackoverflow.com/questions/25628063/…Jeutnarg

2 Answers

0
votes
java.lang.String != hello.Country

In accordance with the documentation, we need to:

  1. Use ResponseBody with consumes

  2. Or create object from String like in REST hello world example

0
votes

I did the following to resolve this issue, using a 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);