4

I'd like to create a Spring Data Repository which should get exposed via Spring Data Rest.

The problem is my "entities" don't come from a database. I have several classes which have a custom annotation and get scanned, this provides some information which I like to expose to the client.

So basically I need something like:

@RestRepository(path="myClasses")
interface MyRepo extends Repository<MyClass, String> {
    List<MyClass> findAll();
}

class MyRepoImpl implements MyRepo {

   List<MyClass> classes;

   MyRepoImpl() {
     // fetch stuff via classpath scanning
     // and save it to "classes"
   }

   @Override
   List<MyClass> findAll() {
     return classes;
   }

}

I now copy & pasted about 10 files from Spring Data MongoDB in order to get a custom @EnableCustomRepositories annotation with a custom FactoryBean, etc. pp. Lot's of stuff. And it still doesn't work...

Is there a simple way of doing this? Of course I could use a custom @Controller, but then I can't use nice rel in my other entities.

I really just need something that extends Repository<T, ID> and create a few custom methods. Or do I have to use CrudRepository so Spring Data Rest can find the findOne and findAll methods?


EDIT:

To be more precise:

My application has a lot of hardcoded Permissions which get used by Spring Security. Each set of permissions has its own class. For example:

@Permission
class UserPermission {
  public final static String RESET_PASSWORD = "USER_RESET_PASSWORD";
  public final static String UPDATE_PROFILE = "USER_UPDATE_PROFILE";
}

Now there's also a persisted class called PermissionGroup which gets persisted to DB. This is basically just:

class PermissionGroup {
  ID id;
  List<String> permissions;
}

What I want is, that I get those typical URLs from Spring Data Rest, which expose my Permissions. So I can use those URL references to add/remove a permission to/from a PermissionGroup. i.e.:

POST http://localhost:8080/app/permissionGroups

{
  "permissions": [
    { "href" : "http://.../permissions/USER_RESET_PASSWORD" },
    { "href" : "http://.../permissions/USER_UPDATE_PROFILE" }
  ]
}
Benjamin M
  • 23,599
  • 32
  • 121
  • 201

1 Answers1

-1

Don't use any annotation or config to use any DB, then no need to specify in your entity class, you have to give implementation in side set methods. -

//@Entity
//@Table(name="myClass")

    class MyClass
    {

    private String name;

    public String getName(){

}

public void setName(){
this.name = "Lisa"
}

}
  • Wow! You've invented the **POJO**. `;-)` ... Now try to use you're code with Spring Data Rest and a Spring Data Repository, and you'll see that Spring Data Rest won't recognize your Repository, because there's no implementation. That's why I copy & pasted over 10 files from Spring Data MongoDB. I just needed a Repository implementation (with `FactoryBean`, `EntityInformation` etc.) that basically does nothing. ... But as I said, it somehow doesn't work **and** I can't imagine why to copy & paste 10 classes just to delete nearly all of its contents. So (I hope) there must be a more simple way. – Benjamin M Apr 23 '14 at 11:08