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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39053663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:02:48  来源:igfitidea点击:

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

javaspringspring-bootspring-data-restspring-hateoas

提问by user2455157

I am having a look into Spring Data REST, specifically the HAL browser. I have been following the documentation at http://docs.spring.io/spring-data/rest/docs/current/reference/html/#_the_hal_browser.

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

When I navigate to http://localhost:8080it redirects me (as expected) to http://localhost:8080/browser/index.html#/, and the HAL browser is displayed. My issue is, rather than this page displaying details about the root of my API, it is trying to display itself. For example, the Response Bodysection has the HTMLof the HAL browser in it, not JSONfrom my API.

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

Screenshot of Response body

响应正文的屏幕截图

I'm not sure if I have done something wrong in my setup - it is pretty vanilla (complete source listing below), so would appreciate any pointers in the right direction!

我不确定我是否在设置中做错了什么 - 它非常简单(下面是完整的源列表),所以希望能有任何正确方向的指示!

For completeness - if I enter /usersinto the Explorertext field and select Go!, then I do see details of my API as expected. Furthermore, if I remove the HAL browser dependency and browse to http://localhost:8080, then I see the root of my API as expected.

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

Screenshot of response when not using HAL browser

Screenshot of response when not using HAL browser

pom.xml

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

应用程序.java

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

User.java

用户.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

用户存储库.java

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

采纳答案by user2455157

Taking the information from the comments on the original question, the answer is as follows:

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

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

更好的奥利弗:
If you ask for HTML, then you get HTML

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

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

回答by user3361807

A simple solution is to copy the hal-browser files for example from the repo: https://github.com/mikekelly/hal-browserin the

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

src/main/resources/static/

src/main/resources/static/

of your Spring Boot Application.

您的 Spring Boot 应用程序。

回答by Nallamachu

As one of the solution suggested in the same page like copy HAL browser files and all. I don't want to make any complexity of that for using REST-HAL-Browser.

作为同一页面中建议的解决方案之一,例如复制 HAL 浏览器文件等。我不想让使用 REST-HAL-Browser 变得如此复杂。

I have done a small R&D to know, how to configure REST-HAL-Browser with Springboot application and How to use it? in a simple and easy manner and made steps available to every one. Even returning data, I am getting in the JSON format than the traditional HTML format.

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

In my application, I used SpringBoot-2.x version and below dependency for REST-HAL-browser.

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

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

Look at hearfor how did I configure and Used.

看看我是如何配置和使用的。

Note:It's better to configure Producer type as application/jsonalong with path, if your method is returning something.

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

回答by shivaspk

If you are using Spring 2.2.X please just use with REST HAL browser just go to : http://localhost:8080/instead of http://localhost:8080/browser.

如果您使用的是 Spring 2.2.X,请仅与 REST HAL 浏览器一起使用,只需转到:http://localhost:8080/而不是http://localhost:8080/browser

Thanks

谢谢