我实施了一个小样本项目来说明我遇到的问题。它位于:
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
但它并没有对响应产生影响,它就像使用默认方式显示实体信息一样。
我不知道自己做错了什么。欢迎任何帮助。
答案 0 :(得分:4)
请参阅以下内容。您的Projection定义是否与相应的实体位于同一个包(或子包)中。
http://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts.projections
Spring Data REST如何找到投影定义?
在与您的实体相同的包中找到的任何
@Projection
接口 定义(或其中一个子包)已注册。您可以手动注册
RepositoryRestConfiguration.getProjectionConfiguration().addProjection(…)
。在任何一种情况下,你的投影界面必须有
@Projection
注释。