对响应应用排序 - Spring Boot

Posted

技术标签:

【中文标题】对响应应用排序 - Spring Boot【英文标题】:Apply Sorting on the Response - Spring Boot 【发布时间】:2019-08-08 06:03:29 【问题描述】:

实际上,我正在返回以下响应(Page-able Response),我想在列表中应用排序,

回应:


"body": 
    "totalCount": 500,
    "success": true,
    "data": [
        
            "id": 3361,
            "displayName": "Kim",
            "events": 57
        ,
        
            "id": 3361,
            "displayName": "Sagar",                
            "events": 57
        ,
        
            "id": 3361,
            "displayName": "Jam",
            "events": 57
        ,
        
            "id": 4779,
            "displayName": "Inga",
            "events": 36
        ,
        
            "id": 4779,
            "displayName": "Mine",
            "events": 36
        ,
        
            "id": 4779,
            "displayName": "Joseph",
            "events": 36
        ,
        
            "id": 3883,
            "displayName": "Amy",
            "events": 10
        ,
        
            "id": 14984,
            "displayName": "Test20",
            "events": 8
        ,
        
            "id": 14984,
            "displayName": "Test20",
            "events": 8
        ,
        
            "id": 14984,
            "displayName": "Test20",
            "events": 8
        ,
        
            "id": 14984,
            "displayName": "Test20",
            "events": 8
        ,
        
            "id": 14984,
            "displayName": "Test20",
            "events": 8
        ,
        
            "id": 14984,
            "displayName": "Test20",
            "events": 8
        ,
        
            "id": 14984,
            "displayName": "Test20 21",
            "events": 8
        ,
        
            "id": 4841,
            "displayName": "Patricia",
            "events": 7
        ,
        
            "id": 3364,
            "displayName": "tutsman",
            "events": 4
        ,
        
            "id": 3364,
            "displayName": "Jaan",
            "events": 4
        
    ]
,
"statusCode": 200

所以问题是,当我请求 API 以及对 displayName 或 id 进行排序时,它有效,但在事件的情况下它不起作用。

PS:我知道它不起作用的原因是事件,显示事件的大小(已经是一个实体),所以我的实体中没有任何大小列.我刚刚加入表格并获取总事件,然后显示,响应中事件的大小,以及 Pageable 对响应进行排序的基本功能必须是列到实体中,但在我的情况下,我只是在计算大小。

尝试过的解决方案:

我尝试使用 stream() 函数对 FacadeImpl 中的响应进行排序,它仅适用于特定页面,但是当我再次请求另一个页面时,排序失败。

PS:所以我必须在数据库级别对元素进行排序,但在数据库中我的表中没有任何大小列

请求网址:

localhost:8080/users/3?page=0&size=20&sort=events,desc

代码:

@Entity
@Access(AccessType.FIELD)
public class Users Serializable 
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
    private List<Event> events;   

    @Column(name = "first_name")
    private String firstName;

    //below are the getter and the setters    

在服务类中:

我正在使用 JPARepo insted of crud 并使用内置方法(findAll)

@Override
@Transactional(readOnly = true)
public Page<Users> find(Filter filter,Pageable pageable) 
    var clientSpecification = specificationGenerator.getClientSpecification(filter);
    return clientDao.findAll(clientSpecification, pageable);

【问题讨论】:

您没有向我们展示执行查询的代码,请将您的存储库函数添加到您的问题中,以便我们可以看到您尝试了什么 我只是在服务类中调用 jpa findAll 内置方法 向我们展示更多代码,例如您的实体和您尝试过的解决方案。代码清楚地说明了可能出现的问题。 好的,让我分享实体 检查更新的代码.. 【参考方案1】:

一种解决方案可以是创建聚合数据的数据库视图,并通过@SecondaryTable 将您的实体链接到此。

例如:

    create view a_summary_view as
    select
   a_id as id, 
   count(*) as b_count 
from b -- where a is your User Entity and b is Events entity

然后:

@Entity
@Table
@SecondaryTable(name = "a_summary_view", 
       pkJoinColumns = @PrimaryKeyJoinColumn(name = "id", referencedColumnName= "id"))
public class Users

   @Column(table = "a_summary_view")
   private Integer eventCount;    
 

这样你就可以在用户实体中有一个字段来排序

【讨论】:

以上是关于对响应应用排序 - Spring Boot的主要内容,如果未能解决你的问题,请参考以下文章

在 Grails 3 应用程序中使用 Spring Security Rest 对“refresh_token”请求的 403 响应

如何在 Spring 中有效地将排序的通量分组到序列化组中?

厉害了,Spring 5 面向响应式编程!

Spring Boot Security OAuth Angular 对预检请求的响应没有通过?

响应式Spring的道法术器(Spring WebFlux 快速上手 + 全面介绍)

会话超时时,如何从 Spring Security 向 angularjs 应用程序发送响应?