3

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.

reiley
  • 3,759
  • 12
  • 58
  • 114
  • If you have IntelliJ IDE check spring components in the bottom tab and see if your path is listed there... – silentsudo Dec 20 '19 at 10:28
  • @silentsudo, I'm using intellij, but not sure where I can see spring components. Can you point me to guide, which can help me? – reiley Dec 20 '19 at 10:31
  • you need to write the controller that listens to the endpoint you are hitting. – joemokenela Dec 20 '19 at 10:41
  • @joemokenela, will that not defeat the purpose of using `RepositoryRestResource`, that we don't have to write the controllers? – reiley Dec 20 '19 at 10:48
  • I didn't notice you are using the RepositoryRestResource annotation, On that note, you need to annotate your CustomProductRepository as well because your method findByDomainName is not exposed. – joemokenela Dec 20 '19 at 11:05
  • @joemokenela, but since we are extending `CustomProductRepository` in `ProductRepository`, doesn't that will make it as a Rest resource? – reiley Dec 20 '19 at 11:18
  • why do you expect `search` in path? – Ori Marko Dec 23 '19 at 15:25
  • As per documentation, all exposed APIs will be mentioned in path. Regardless, question is how should i implement custom rest API within this code. – reiley Dec 23 '19 at 15:27
  • 2
    See https://stackoverflow.com/questions/25201306/implementing-custom-methods-of-spring-data-repository-and-exposing-them-through – Mafor Dec 25 '19 at 16:58

1 Answers1

1

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...

Mafor
  • 9,668
  • 2
  • 21
  • 36