// 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 11:06:43 浏览: 317
这段代码使用 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 Material 提供的 `MatPaginator` 组件来实现分页。具体实现步骤如下:
1. 在组件的 HTML 模板中,添加 `MatPaginator` 组件。例如:
```html
<mat-paginator [length]="auditingApprovalRecords.length" [pageSize]="pageSize" [pageSizeOptions]="pageSizeOptions" (page)="onAuditingPageChange($event)">
</mat-paginator>
```
其中,`length` 表示数据总数,`pageSize` 表示每页显示的数据条数,`pageSizeOptions` 表示用户可选的每页数据条数选项,`page` 事件表示当用户点击页码时的回调函数。
2. 在组件的 TypeScript 文件中,定义相应的属性和方法。例如:
```typescript
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { IApprovalRecord } from 'app/shared/model/approval-record.model';
import { ApprovalRecordService } from 'app/entities/approval-record/approval-record.service';
@Component({
selector: 'jhi-approval-record',
templateUrl: './approval-record.component.html',
})
export class ApprovalRecordComponent implements OnInit {
@ViewChild(MatPaginator) paginator!: MatPaginator;
auditingApprovalRecords: IApprovalRecord[] = [];
createdApprovalRecords: IApprovalRecord[] = [];
auditedApprovalRecords: IApprovalRecord[] = [];
pageSize = 10;
pageSizeOptions: number[] = [5, 10, 20];
constructor(private approvalRecordService: ApprovalRecordService) {}
ngOnInit(): void {
this.loadAll();
}
loadAll(): void {
this.approvalRecordService.queryAuditing().subscribe((res: HttpResponse<IApprovalRecord[]>) => {
this.auditingApprovalRecords = res.body ?? [];
this.auditingApprovalRecords.paginator = this.paginator; // 绑定分页组件
});
this.approvalRecordService.queryCreated().subscribe((res: HttpResponse<IApprovalRecord[]>) => {
this.createdApprovalRecords = res.body ?? [];
this.createdApprovalRecords.paginator = this.paginator; // 绑定分页组件
});
this.approvalRecordService.queryAudited().subscribe((res: HttpResponse<IApprovalRecord[]>) => {
this.auditedApprovalRecords = res.body ?? [];
this.auditedApprovalRecords.paginator = this.paginator; // 绑定分页组件
});
}
onAuditingPageChange(event: any): void {
this.auditingApprovalRecords.paginator = this.paginator;
}
onCreatedPageChange(event: any): void {
this.createdApprovalRecords.paginator = this.paginator;
}
onAuditedPageChange(event: any): void {
this.auditedApprovalRecords.paginator = this.paginator;
}
}
```
其中,`@ViewChild(MatPaginator) paginator!: MatPaginator` 表示获取 `MatPaginator` 组件的实例。在 `loadAll()` 方法中,将获取到的数据绑定到 `MatPaginator` 组件上。分别定义了三个回调函数 `onAuditingPageChange`、`onCreatedPageChange` 和 `onAuditedPageChange`,用来处理分页变化事件。
注意:在绑定分页组件时,需要先将获取到的数据赋值给对应的属性,再将属性绑定到分页组件上。
如何在以下代码中加入分页的功能 后端代码:@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);
```
这样,你就成功地实现了分页功能。
阅读全文