Tools

The HAL Explorer

Kai Tödter 创建了一个有用的应用程序:HAL Explorer ( Git RepoReference DocDemo)。这是一个基于 Angular 的 Web 应用程序,可以让您轻松地探索 HAL 和基于 HAL-FORMS 的 HTTP 响应。它还支持 Spring Data REST 生成的 Spring 配置文件。您可以将它指向任何 Spring Data REST API,并使用它来浏览应用程序和创建新资源。

Kai Tödter has created a useful application: HAL Explorer (Git Repo, Reference Doc, Demo). It is an Angular based web application that lets you easily explore HAL and HAL-FORMS based HTTP responses. It also supports Spring profiles generated by Spring Data REST. You can point it at any Spring Data REST API and use it to navigate the app and create new resources.

你不再需要提取文件、将它们嵌入到你的应用程序中并编写一个 Spring MVC 控制器来提供给它们,你所需要做的就是添加一个依赖项。

Instead of pulling down the files, embedding them in your application, and crafting a Spring MVC controller to serve them up, all you need to do is add a single dependency.

以下清单显示了如何在 Maven 中添加依赖项:

The following listing shows how to add the dependency in Maven:

<dependencies>
  <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-rest-hal-explorer</artifactId>
  </dependency>
</dependencies>

以下清单显示了如何在 Gradle 中添加依赖项:

The following listing shows how to add the dependency in Gradle:

dependencies {
    implementation 'org.springframework.data:spring-data-rest-hal-explorer'
}

如果你使用 Spring Boot 或 Spring Data BOM(材料清单),则无需指定版本。

If you use Spring Boot or the Spring Data BOM (bill of materials), you do not need to specify the version.

此依赖项自动将 HAL Explorer 配置为在浏览器中访问应用程序根 URI 时提供服务。(注意:[role="bare"][role="bare"]http://localhost:8080/api插入了浏览器,并重定向到下图中显示的 URL。)

This dependency auto-configures the HAL Explorer to be served up when you visit your application’s root URI in a browser. (NOTE: [role="bare"]http://localhost:8080/api was plugged into the browser, and it redirected to the URL shown in the following image.)

hal explorer 1

前面的屏幕截图显示了 API 的根路径。右侧是响应中的详细信息,包括标头和正文(HAL 文档)。

The preceding screenshot shows the root path of the API. On the right side are details from the response, including headers, and the body (a HAL document).

HAL Explorer 从响应中读取链接,并将它们放在左侧的列表中。你可以点击绿色的 GET 按钮并导航到其中一个集合,或点击其他按钮来进行更改(POST、PUT、PATCH)或删除资源。

The HAL Explorer reads the links from the response and puts them in a list on the left side. You can either click on the green GET button and navigate to one of the collections, or click on the other buttons to make changes (POST, PUT, PATCH) or delete resources.

HAL Explorer 理解 URI 模板。每当链接包含 URI 模板时,就会弹出一个模态对话框,你可以在其中输入模板参数。

The HAL Explorer understands URI Templates. Whenever a link contains a URI template, a modal dialog pops up where you can enter the template parameters.

hal explorer 3

如果您点击 Go!,而不输入任何内容,这些变量实际上会被忽略。对于 Projections and ExcerptsPaging and Sorting等情况,这可能十分有用。

If you click Go! without entering anything, the variables are essentially ignored. For situations like Projections and Excerpts or Paging and Sorting, this can be useful.

当你点击带有 +> 符号的 NON-GET 按钮时,会出现一个模态对话框。该对话框显示属于所单击按钮的 HTTP 方法。你可以填充正文并提交新的 JSON 文档。

When you click on a NON-GET button with a + or a > sign on it, a modal dialog appears. It shows the HTTP method belonging to the clicked button. You can fill the body and submit the new JSON document.

在 URI 和 HTTP 方法的下方是字段。根据 Spring Data REST 自动生成的资源的元数据,这些字段会自动提供。如果你更新你的域对象,则弹出框会反映它,如下面的图像所示:

Below the URI and HTTP method are the fields. These are automatically supplied, depending on the metadata of the resources, which was automatically generated by Spring Data REST. If you update your domain objects, the pop-up reflects it, as the following image shows:

hal explorer 2