Search code examples
javaspring-bootspring-data-restspring-hateoas

@Projection not working


I've implemented a little sample project to illustrate the problem I have. It's located here:

https://github.com/jvillane/spring-boot-hateoas-rest

What i'm trying to do is creating several @Projection's of the same Entity:

@Projection(name = "S", types = User.class)
public interface UserS {
    String getName();
}

@Projection(name = "M", types = User.class)
public interface UserM {
    String getName();
    String getDni();
}

@Projection(name = "L", types = User.class)
public interface UserL {
    String getName();
    String getDni();
    Country getCountry();
}

And using them to obtain more or less Entity information by calling (with and without quotes):

http://localhost:8080/api/users/1?projection=S
http://localhost:8080/api/users/1?projection=M
http://localhost:8080/api/users/1?projection=L

But it doesn't make a difference in the response, it's like it's using the default way to show the Entity info.

I don't know what i'm doing wrong. Any help is welcome.


Solution

  • See the following. Is your Projection definition in the same package (or subpackage) as the corresponding entity.

    http://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts.projections

    How does Spring Data REST finds projection definitions?

    Any @Projection interface found in the same package as your entity definitions (or one of it’s sub-packages) is registered.

    You can manually register via RepositoryRestConfiguration.getProjectionConfiguration().addProjection(…).

    In either situation, the interface with your projection MUST have the @Projection annotation.