Spring 数据 REST - HAL 浏览器 - 返回 HAL 浏览器 HTML 而不是 API 的根

Spring Data REST - HAL browser - returning HAL browser HTML rather than root of API

我正在查看 Spring Data REST,特别是 HAL 浏览器。我一直在关注 http://docs.spring.io/spring-data/rest/docs/current/reference/html/#_the_hal_browser.

上的文档

当我导航到 http://localhost:8080 时,它会将我重定向(如预期的那样)到 http://localhost:8080/browser/index.html#/,并显示 HAL 浏览器。我的问题是,这个页面没有显示有关我的 API 根目录的详细信息,而是试图显示自己。例如,Response Body 部分包含 HAL 浏览器的 HTML,而不是我的 API.

中的 JSON

我不确定我是否在我的设置中做错了什么 - 它非常普通(下面列出了完整的源代码),所以非常感谢任何正确方向的指示!

为了完整性 - 如果我在 Explorer 文本字段中输入 /users 和 select Go!,那么我会看到我的 [=62] 的详细信息=] 正如预期的那样。此外,如果我删除 HAL 浏览器依赖项并浏览到 http://localhost:8080,那么我会按预期看到 API 的根目录。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>sample</groupId>
    <artifactId>restsample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>My application</name>
    <description>My application description</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-hal-browser</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Application.java

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

User.java

@Data
@Entity
public class User {
    @Id
    @GeneratedValue
    private Long id;

    @NotBlank
    @Size(min = 1, max = 100)
    @Column(unique = true)
    private String username;
}

UserRepository.java

@RepositoryRestResource
public interface UserRepository extends CrudRepository<User, Long> {
    User save(User user);
}

一个简单的解决方案是从存储库中复制 hal-browser 文件: https://github.com/mikekelly/hal-browser

src/main/resources/static/

您的 Spring 启动应用程序。

取原题评论中的信息,答案如下:

a-better-oliver:
If you ask for HTML, then you get HTML

我:
I should have used "Accept: application/hal+json", which does indeed return the correct data.

作为同一页面中建议的解决方案之一,例如复制 HAL 浏览器文件和所有内容。我不想因为使用 REST-HAL-Browser 而使它变得复杂。

我做了一个小研发,想知道如何使用 Springboot 应用程序配置 REST-HAL-Browser 以及如何使用它?以一种简单易行的方式,并使每个人都可以使用步骤。即使返回数据,我得到的是 JSON 格式,而不是传统的 HTML 格式。

在我的应用程序中,我使用了 SpringBoot-2.x 版本和以下版本的 REST-HAL-browser 依赖项。

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

Look at hear我是怎么配置和使用的

注意: 如果您的方法返回某些内容,最好将生产者类型配置为 application/json 以及路径。

如果您正在使用 Spring 2.2.X 请仅使用 REST HAL 浏览器,只需转到:http://localhost:8080/ instead of http://localhost:8080/browser.

谢谢

尝试更改

的依赖项
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-rest-hal-browser</artifactId>
</dependency>

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

然后转到:http://localhost:8080

对于未来的读者:根据 Spring-Io GitHub 问题,HAL 浏览器在较新版本的 Spring 中 已弃用

如果您在使用 Hal 浏览器时遇到问题,请尝试使用 Hal Explorerhttps://mvnrepository.com/artifact/org.springframework.data/spring-data-rest-hal-explorer

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

可以在这个 GitHub 问题中找到更多信息: Spring Boot 2.2 apps should use spring-data-rest-hal-explorer rather than spring-data-rest-hal-browser

在 Spring 引导中更改上下文路径确实很混乱 spring-data-rest-hal-explorer。期望 /mycontext/explorer/index.html 但得到 404。

所以我从 pom.xml 中删除了 spring-data-rest-hal-explorer。然后添加:

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>hal-explorer</artifactId>
            <version>1.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>webjars-locator</artifactId>
            <version>0.42</version>
        </dependency>

之后我可以访问 hal explorer: /mycontext/webjars/hal-explorer/index.html