Only expose certain fields when viewing specific item with Spring Data?

后端 未结 2 1958
[愿得一人]
[愿得一人] 2021-01-16 21:21

I\'m currently using Spring Boot to create a REST API with a mongodb backend. Is it possible to only expose certain fields when viewing a specific item, and not a list of it

2条回答
  •  孤独总比滥情好
    2021-01-16 22:12

    You have to add @Query annotation on the find method in the repository and specify the fields parameter:

    public interface PersonRepository extends MongoRepository
    
      @Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}")
      List findByThePersonsFirstname(String firstname);
    
    }
    

    See: http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongodb.repositories.queries.json-based

提交回复
热议问题