3
votes

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.

When I navigate to http://localhost:8080 it 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 Body section has the HTML of the HAL browser in it, not JSON from my 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 /users into the Explorer text 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.

Screenshot of response when not using HAL browser

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);
}
6
If you ask for HTML, then you get HTML.a better oliver
Thank you for the reply - however even if I add the application/json+hal content type header to the "Custom Request Headers" section of the HAL browser, I still get the HAL browser itself returned as the response. Furthermore, there is absolutely no indication in the documentation that this should be needed (and any examples I have seen of a running HAL browser do not appear to need to pass the content type header either).user2455157
My mistake, I should have used "Accept: application/hal+json", which does indeed return the correct data. So, I guess the issue is the way that the spring boot package puts the redirect to the HAL browser on the same endpoint as the API, thus by default it won't work as expected.user2455157

6 Answers

2
votes

Try changing dependency from

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

to

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

and then goto: http://localhost:8080

0
votes

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

src/main/resources/static/

of your Spring Boot Application.

0
votes

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

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

0
votes

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.

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.

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

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

Look at hear for how did I configure and Used.

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

0
votes

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.

Thanks

0
votes

For future readers: HAL Browser is deprecated in newer versions of Spring Boot as per their Spring-Io GitHub issue.

If you face issues with Hal Browser, please try using the Hal Explorer instead: https://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>

More information can be found in this GitHub issue : Spring Boot 2.2 apps should use spring-data-rest-hal-explorer rather than spring-data-rest-hal-browser