@Projection 不工作

@Projection not working

我已经实现了一个小示例项目来说明我遇到的问题。它位于此处:

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

我想做的是创建多个 @Projection 的同一实体:

@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();
}

并使用它们通过调用(带引号和不带引号)获取或多或少的实体信息:

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

但这对响应没有影响,就像它使用默认方式显示实体信息一样。

我不知道我做错了什么。欢迎任何帮助。

见下文。你的Projection定义是不是和对应实体在同一个包(或子包)里。

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.