Reputation: 3761
I'm working on a spring-boot project which uses @RepositoryRestResource
.
There are 2 entities, Product
& Domain
, which has many-to-many relation.
I want to implement a custom REST call which is running a complex query.
To implement custom implementation I'm following this blog.
ProductRepository Interface:
@RepositoryRestResource(collectionResourceRel = "product", path = "product")
public interface ProductRepository extends CrudRepository<Product, Long>, CustomProductRepository {
List<Product> findByName(@Param("name") String name);
}
CustomProductRepository.java - Interface for custom REST call
public interface CustomProductRepository {
public List<Product> findByDomainName(String domainName);
}
ProductRepositoryImpl.java - It's implementation. (I'm following the naming convention as mentioned in the blog)
public class ProductRepositoryImpl implements CustomProductRepository {
@Override
public List<Product> findByDomainName(long id, String domainName) {
List<Product> result = new ArrayList<>();
// Query logic goes here
return result;
}
}
Updating ProductRepository.java to extend CustomProductRepository
@RepositoryRestResource(collectionResourceRel = "product", path = "product")
public interface ProductRepository extends CrudRepository<Product, Long>, CustomProductRepository {
List<Product> findByName(@Param("name") String name);
}
The application starts without error. I expect when I call http://localhost:8080/product/search
, it should list my custom method: findByDomainName
. But it doesn't.
All I get is:
{
"_links" : {
"findByName" : {
"href" : "http://localhost:8080/product/search/findByName{?name}",
"templated" : true
},
"self" : {
"href" : "http://localhost:8080/product/search/"
}
}
}
Even when I call make a REST call to http://localhost:8080/product/search/findByDomainName, I get 404.
What am I missing?
Thanks.
Upvotes: 3
Views: 572
Reputation: 10681
Apparently it is not possible by design. The custom method would work though, if annotated with @Query
.
See implementing-custom-methods-of-spring-data-repository-and-exposing-them...
Upvotes: 1