@Projection 不起作用

Jul*_*ane 2 java spring-data-rest spring-hateoas spring-boot

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

https://github.com/jvillane/spring-boot-hateoas-rest
Run Code Online (Sandbox Code Playgroud)

我想要做的是创建同一个实体的几个@Projection

https://github.com/jvillane/spring-boot-hateoas-rest
Run Code Online (Sandbox Code Playgroud)

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

@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();
}
Run Code Online (Sandbox Code Playgroud)

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

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

Ala*_*Hay 7

请参阅以下内容。您的 Projection 定义是否在与相应实体相同的包(或子包)中。

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

Spring Data REST 如何找到投影定义?

@Projection在与您的实体定义(或其子包之一)相同的包中找到的任何接口都已注册。

您可以通过 手动注册 RepositoryRestConfiguration.getProjectionConfiguration().addProjection(…)

在任何一种情况下,与您的投影的界面都必须有 @Projection注释。