나는 Spring Data REST
을 사용하여 노출 된 간단한 UserRepository
을 가지고 있습니다.스프링 데이터 REST : 단일 리소스의 투영 표현
@Document(collection = User.COLLECTION_NAME)
@Setter
@Getter
public class User extends Entity {
public static final String COLLECTION_NAME = "users";
private String name;
private String email;
private String password;
private Set<UserRole> roles = new HashSet<>(0);
}
나는 다음과 같은 방법을 보이는 UserProjection
클래스를 만들었습니다 : 나는 '
@RepositoryRestResource(collectionResourceRel = User.COLLECTION_NAME, path = RestPath.Users.ROOT,
excerptProjection = UserProjection.class)
public interface RestUserRepository extends MongoRepository<User, String> {
// Not exported operations
@RestResource(exported = false)
@Override
<S extends User> S insert(S entity);
@RestResource(exported = false)
@Override
<S extends User> S save(S entity);
@RestResource(exported = false)
@Override
<S extends User> List<S> save(Iterable<S> entites);
}
: 여기
@JsonInclude(JsonInclude.Include.NON_NULL)
@Projection(types = User.class)
public interface UserProjection {
String getId();
String getName();
String getEmail();
Set<UserRole> getRoles();
}
이 저장소 클래스를 여기 는
User
엔티티 클래스 구성에서 사용자 투영을 지정하여 사용되는지 확인했습니다. 내가 할 때
config.getProjectionConfiguration().addProjection(UserProjection.class, User.class);
그래서, 이 /사용자에을 GET 경로를 내가 응답 다음 (투사가 적용됩니다) 얻을 :
{
"_embedded" : {
"users" : [ {
"name" : "Yuriy Yunikov",
"id" : "5812193156aee116256a33d4",
"roles" : [ "USER", "ADMIN" ],
"email" : "[email protected]",
"points" : 0,
"_links" : {
"self" : {
"href" : "http://127.0.0.1:8080/users/5812193156aee116256a33d4"
},
"user" : {
"href" : "http://127.0.0.1:8080/users/5812193156aee116256a33d4{?projection}",
"templated" : true
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://127.0.0.1:8080/users"
},
"profile" : {
"href" : "http://127.0.0.1:8080/profile/users"
}
},
"page" : {
"size" : 20,
"totalElements" : 1,
"totalPages" : 1,
"number" : 0
}
}
을하지만, 내가 만들려고 할 때 GET은 단일 리소스를 요구합니다. /사용자/5812193156aee116256a33d4, 나는 다음과 같은 응답을 얻을 :
{
"name" : "Yuriy Yunikov",
"email" : "[email protected]",
"password" : "123456",
"roles" : [ "USER", "ADMIN" ],
"_links" : {
"self" : {
"href" : "http://127.0.0.1:8080/users/5812193156aee116256a33d4"
},
"user" : {
"href" : "http://127.0.0.1:8080/users/5812193156aee116256a33d4{?projection}",
"templated" : true
}
}
}
당신이 볼 수있는 바와 같이, 암호 필드가 돌려지고 투사가 적용되지 않습니다. 나는 민감한 자료의 자료를 숨길 수있는 @JsonIgnore
어노테이션이 있다는 것을 알고있다. 그러나 내 User
개체는 API 또는 JSON 표현에 대해 알지 못하는 다른 응용 프로그램 모듈에 있으므로 주석 필드에 @JsonIgnore
주석을 표시하는 것은 의미가 없습니다.
@Oliver Gierke의 here이 발췌 록이 단일 자원에 자동으로 적용되지 않는 이유에 대한 게시물을 보았습니다. 그러나, 그것은 여전히 내 경우에 매우 불편하고 단일 리소스를 얻을 때 같은 UserProjection
을 반환하고 싶습니다. 어떻게 든 사용자 정의 컨트롤러를 만들거나 @JsonIgnore
필드를 표시하지 않고 그것을 할 수 있습니까?
'ResourceProcessor'를 사용하여이 효과 (또는 비슷한 효과)를 얻을 수 있습니다. https://jira.spring.io/browse/DATAREST-428의 댓글에 잠재적으로 유용한 토론이 있습니다. – CollinD
@CollinD 훌륭한 링크를 제공해 주셔서 감사합니다! 이런 경우에는'ResourceProcessor' 나'ResourceAssembler'가 맞는 해결책이 될 것 같습니다. 그러나 나는 아직도 Spring Data REST에서 주석이나 설정이 없다는 것에 대해 궁금해한다. –