// This code sample uses the 'Unirest' library: // http://unirest.io/java.html HttpResponse<JsonNode> response = Unirest.get("https://your-domain.atlassian.net/rest/api/2/search") .basicAuth("email@example.com", "<api_token>") .header("Accept", "application/json") .queryString("jql", "project = HSP") .asJson(); System.out.println(response.getBody());
时间: 2023-02-06 08:06:43 浏览: 322
这段代码使用 Unirest 库发送了一个 HTTP GET 请求到 "https://your-domain.atlassian.net/rest/api/2/search",请求头中带有基本身份认证(使用你提供的电子邮件地址和 API 令牌)和 Accept 请求头,并在请求参数中提供了 JQL 查询 "project = HSP"。请求返回的内容会以 JSON 的形式被打印出来。
相关问题
以下三个方法已经取出了我想要的对应的数据现在想要对这些数据分别进行分页处理我应该怎么修改代码 loadAll(): void { this.approvalRecordService.queryAuditing().subscribe((res: HttpResponse<IApprovalRecord[]>) => { this.auditingApprovalRecords = res.body ?? []; }); this.approvalRecordService.queryCreated().subscribe((res: HttpResponse<IApprovalRecord[]>) => { this.createdApprovalRecords = res.body ?? []; }); this.approvalRecordService.queryAudited().subscribe((res: HttpResponse<IApprovalRecord[]>) => { this.auditedApprovalRecords = res.body ?? []; }); }
您可以使用 Angular 中的内置分页器来对数据进行分页处理。以下是您可以尝试的代码示例:
首先,您需要在组件中设置每个分页器的页面大小,例如:
```typescript
// Set the page size for each pagination
pageSize = 10;
```
然后,您可以使用 `slice` 方法对每个数组进行分页处理:
```typescript
// Slice the auditingApprovalRecords array based on the current page and page size
auditingApprovalRecordsPage = this.auditingApprovalRecords.slice((this.auditingPage - 1) * this.pageSize, this.auditingPage * this.pageSize);
// Slice the createdApprovalRecords array based on the current page and page size
createdApprovalRecordsPage = this.createdApprovalRecords.slice((this.createdPage - 1) * this.pageSize, this.createdPage * this.pageSize);
// Slice the auditedApprovalRecords array based on the current page and page size
auditedApprovalRecordsPage = this.auditedApprovalRecords.slice((this.auditedPage - 1) * this.pageSize, this.auditedPage * this.pageSize);
```
在这里,`auditingPage`、`createdPage`和`auditedPage`是当前页码,您需要在组件中设置它们并将其绑定到分页器。
最后,您可以在模板中使用 Angular 内置的分页器组件来显示分页器并显示分页后的数据,例如:
```html
<!-- Show pagination and data for auditingApprovalRecords -->
<ngb-pagination [(page)]="auditingPage" [pageSize]="pageSize" [collectionSize]="auditingApprovalRecords.length"></ngb-pagination>
<div *ngFor="let record of auditingApprovalRecordsPage">
<!-- display record data here -->
</div>
<!-- Show pagination and data for createdApprovalRecords -->
<ngb-pagination [(page)]="createdPage" [pageSize]="pageSize" [collectionSize]="createdApprovalRecords.length"></ngb-pagination>
<div *ngFor="let record of createdApprovalRecordsPage">
<!-- display record data here -->
</div>
<!-- Show pagination and data for auditedApprovalRecords -->
<ngb-pagination [(page)]="auditedPage" [pageSize]="pageSize" [collectionSize]="auditedApprovalRecords.length"></ngb-pagination>
<div *ngFor="let record of auditedApprovalRecordsPage">
<!-- display record data here -->
</div>
```
这将显示三个分页器,每个分页器都与相应的数组进行交互,并在每个分页器下方显示分页后的数据。
如何在以下代码中加入分页的功能 后端代码:@Query( "select approvalRecord from ApprovalRecord approvalRecord inner join ApprovalRecordItem approvalRecordItem on approvalRecord = approvalRecordItem.approvalRecord where approvalRecordItem.status <> 'AUDITING' and approvalRecordItem.createdBy.login = ?#{principal.username} order by approvalRecordItem.id desc" ) List<ApprovalRecord> findAuditedByCurrentUser(); @GetMapping("/approval-records/my-created") public List<ApprovalRecord> getAllCreatedApprovalRecords() { log.debug("REST request to get all getAllCreatedApprovalRecords"); return approvalRecordRepository.findByCreatedByIsCurrentUser(); } 前段代码: loadAll(): void { this.approvalRecordService.queryAuditing().subscribe((res: HttpResponse<IApprovalRecord[]>) => { this.auditingApprovalRecords = res.body ?? []; }); this.approvalRecordService.queryCreated().subscribe((res: HttpResponse<IApprovalRecord[]>) => { this.createdApprovalRecords = res.body ?? []; }); this.approvalRecordService.queryAudited().subscribe((res: HttpResponse<IApprovalRecord[]>) => { this.auditedApprovalRecords = res.body ?? []; }); }
首先,你需要在后端代码中添加分页相关的参数和逻辑。你可以使用 Spring Data JPA 提供的 Pageable 接口来实现分页。修改你的查询方法,使其接受一个 Pageable 参数,然后在查询中使用该参数。例如:
```java
@Query("select approvalRecord from ApprovalRecord approvalRecord inner join ApprovalRecordItem approvalRecordItem on approvalRecord = approvalRecordItem.approvalRecord where approvalRecordItem.status <> 'AUDITING' and approvalRecordItem.createdBy.login = ?#{principal.username} order by approvalRecordItem.id desc")
List<ApprovalRecord> findAuditedByCurrentUser(Pageable pageable);
```
接下来,在前端代码中添加分页的 UI 组件和逻辑。你可以使用 Angular Material 提供的 MatPaginator 组件来实现分页。首先,在 HTML 模板中添加 MatPaginator 组件:
```html
<mat-paginator [length]="totalItems" [pageSize]="pageSize" [pageIndex]="pageIndex" [pageSizeOptions]="pageSizeOptions" (page)="loadPage($event)"></mat-paginator>
```
然后,在对应的 TypeScript 文件中添加分页相关的属性和方法:
```typescript
totalItems: number;
pageSize = 10;
pageSizeOptions = [5, 10, 20];
pageIndex = 0;
loadPage(event: PageEvent): void {
this.pageIndex = event.pageIndex;
this.pageSize = event.pageSize;
this.loadAll();
}
loadAll(): void {
this.approvalRecordService.queryAuditing({page: this.pageIndex, size: this.pageSize}).subscribe((res: HttpResponse<IApprovalRecord[]>) => {
this.auditingApprovalRecords = res.body ?? [];
this.totalItems = res.headers.get('X-Total-Count');
});
this.approvalRecordService.queryCreated({page: this.pageIndex, size: this.pageSize}).subscribe((res: HttpResponse<IApprovalRecord[]>) => {
this.createdApprovalRecords = res.body ?? [];
});
this.approvalRecordService.queryAudited({page: this.pageIndex, size: this.pageSize}).subscribe((res: HttpResponse<IApprovalRecord[]>) => {
this.auditedApprovalRecords = res.body ?? [];
});
}
```
在 loadAll 方法中,我们将分页相关的参数传递给查询方法,并在响应中获取分页相关的信息,比如总记录数。然后,我们将总记录数赋值给 totalItems 属性,以便 MatPaginator 组件能够正确显示分页信息。
最后,你需要在后端代码中修改查询方法,使其返回一个 Page 对象,而不是 List。例如:
```java
@Query("select approvalRecord from ApprovalRecord approvalRecord inner join ApprovalRecordItem approvalRecordItem on approvalRecord = approvalRecordItem.approvalRecord where approvalRecordItem.status <> 'AUDITING' and approvalRecordItem.createdBy.login = ?#{principal.username} order by approvalRecordItem.id desc")
Page<ApprovalRecord> findAuditedByCurrentUser(Pageable pageable);
```
这样,你就成功地实现了分页功能。
阅读全文