Spring Data Rest - sort by nested property

后端 未结 3 1469
[愿得一人]
[愿得一人] 2021-01-08 00:56

I have a database service using Spring Boot 1.5.1 and Spring Data Rest. I am storing my entities in a MySQL database, and accessing them over REST using Spring\'s PagingAndS

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-08 01:56

    From Spring Data REST documentation:

    Sorting by linkable associations (that is, links to top-level resources) is not supported.

    https://docs.spring.io/spring-data/rest/docs/current/reference/html/#paging-and-sorting.sorting

    An alternative that I found was use @ResResource(exported=false). This is not valid (expecially for legacy Spring Data REST projects) because avoid that the resource/entity will be loaded HTTP links:

    JacksonBinder
    BeanDeserializerBuilder updateBuilder throws
     com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of ' com...' no String-argument constructor/factory method to deserialize from String value
    

    I tried activate sort by linkable associations with help of annotations but without success because we need always need override the mappPropertyPath method of JacksonMappingAwareSortTranslator.SortTranslator detect the annotation:

                if (associations.isLinkableAssociation(persistentProperty)) {
                    if(!persistentProperty.isAnnotationPresent(SortByLinkableAssociation.class)) {
                        return Collections.emptyList();
                    }
                }
    

    Annotation

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface SortByLinkableAssociation {
    }
    

    At project mark association as @SortByLinkableAssociation:

    @ManyToOne
    @SortByLinkableAssociation
    private Name name;
    

    Really I didn't find a clear and success solution to this issue but decide to expose it to let think about it or even Spring team take in consideration to include at nexts releases.

提交回复
热议问题