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

问题描述 投票:2回答:3

我正在研究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

Screenshot of Response body

我不确定我的设置是否做错了 - 它非常香草(下面的完整源代码清单),所以会欣赏正确方向的任何指针!

为了完整 - 如果我将/users输入到Explorer文本字段并选择Go !,那么我确实会看到我的API的详细信息。此外,如果我删除HAL浏览器依赖项并浏览到http://localhost:8080,那么我会按预期看到我的API的根目录。

Screenshot of response when not using HAL browser

pom.hml

<?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;
}

user repository.Java

@RepositoryRestResource
public interface UserRepository extends CrudRepository<User, Long> {
    User save(User user);
}
java spring spring-boot spring-data-rest spring-hateoas
3个回答
0
投票

一个简单的解决方案是复制hal-browser文件,例如从repo:https://github.com/mikekelly/hal-browser中复制

src/main/resources/static/

您的Spring Boot应用程序。


0
投票

从原始问题的评论中获取信息,答案如下:

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.


0
投票

作为同一页面中建议的解决方案之一,如复制HAL浏览器文件和所有。我不想让使用REST-HAL-Browser的任何复杂性。

我已经做了一个小的研发来了解,如何使用Springboot应用程序配置REST-HAL-Browser以及如何使用它?以简单方便的方式为每个人提供步骤。即使返回数据,我也会使用JSON格式而不是传统的HTML格式。

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

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

Look at hear我是如何配置和使用的。

注意:如果您的方法返回一些内容,最好将Producer类型配置为application / json以及path。

© www.soinside.com 2019 - 2024. All rights reserved.