2

We are building an API for our service and we would like to leverage Spring Data Rest as much as possible.

This API and the new model underneath will substitute a legacy API (and it's old model) that we still need to support. Our idea is to build an "adapter" web app that replicates the structure of the old api and serve the old model using some internal transformations.

Also the old api is using Spring Data Rest, so here the idea: build a repository implementation that instead of querying a database will query our brand new API, retrieve the new model, apply some transformations, and return the old model.

Unfortunately, even if I'm annotating the repository implementation with the @Repository annotation, Spring is not exposing the repository in the API.

I'm not sure if this is actually something possible to do or is just a matter of me not implementing some core functionalities. What I would like to avoid is reimplement all spring data rest methods manually in a controller.

Here my Repository class

// Method are not implemented, this is just the backbone
@Repository
public class SampleRespositoryImpl implements ReadOnlyRepository<OldSample, String> {

    NewApiClient client;

    public SampleRespositoryImpl(NewApiClient client) {
        this.client = client;
    }

    @Override
    public OldSample findOne(String accession) {
        NewSample newSample = client.fetch(accession)
        OldSample oldSample = //apply transformation to newSample
        return oldSample;
    }

    @Override
    public boolean exists(String accession) {
        return client.fetch(accession) != null;
    }

    @Override
    public Iterable<OldSample> findAll() {
        return new ArrayList<>();
    }

    @Override
    public Iterable<OldSample> findAll(Iterable<String> var1) {
        return new ArrayList<>();
    }

    @Override
    public long count() {
        return 0;
    }

    @Override
    public Iterable<OldSample> findAll(Sort var1) {
        return new ArrayList<>();
    }

    @Override
    public Page<OldSample> findAll(Pageable var1) {
        List<OldSample> OldSampleList = new ArrayList<>();
        Page<OldSample> page = new PageImpl<>(OldSampleList);
        return page;
    }
}

Here what I would like to get back when I hit the api root (http://localhost:8080/)

{
    "_links": {
        "samples": {
            "href": "http://localhost:8080/samples{?page,size,sort}
        }
    }   
}
Kerruba
  • 1,779
  • 1
  • 17
  • 25
  • 1
    Possible duplicate of [Implementing custom methods of Spring Data repository and exposing them through REST](http://stackoverflow.com/questions/25201306/implementing-custom-methods-of-spring-data-repository-and-exposing-them-through) – Kevin Peters Apr 19 '17 at 11:41

1 Answers1

0

Someone else linked me to another answer in StackOverflow available here as possible duplication.

Reading through that answer, I decided that is too much effort to follow this path for our needs, so I'm more oriented to create a custom controller to expose necessary methods.

This solution was reported by Kevin as answer to Implementing methods of Spring Data repository and exposing them through REST

Community
  • 1
  • 1
Kerruba
  • 1,779
  • 1
  • 17
  • 25