How to add a specific field in Spring Data Rest?

后端 未结 1 639
生来不讨喜
生来不讨喜 2021-01-29 02:43

I am developing a web service using Spring Data Rest.

public interface BookRepository extends PagingAndSortingRepository {

    @Override
    @         


        
相关标签:
1条回答
  • 2021-01-29 03:13

    You can use projection in the output of your repo method.

    1. So in your case you can set a projection, for example:
    @Projection(name = "BookWithRating", types = { Book.class }) 
    interface BookWithRating { 
    
      Float getRating(); 
    
      Book getBook(); 
    }
    
    1. Then setup a query method:
    @Query("select avg(rl.rating) as rating, b as book from ReadingList rl join rl.book b group by rl.book order by rating desc")
    Page<BookWithRating> findAllWithRating(Pageable pageable);
    

    Pay attention to the alias of the output parameters - their names must match the projection getters.

    Also you can try to use this technics for enriching a data model (see how to 'annotate exposed properties with @Value using SpEL expressions to expose synthetic properties').

    0 讨论(0)
提交回复
热议问题