# HATEOS

## Introduction

* HATEOS (Hypermedia as the Engine of Application State)  can used in API end point and display relevant api end point

## Configuration&#x20;

* porn.xml

```markup
<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
```

## Model

* Extend the modal of HATEOS

```java
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import org.springframework.hateoas.RepresentationModel;
import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserInfo extends RepresentationModel<UserInfo>{
    private Long id;
    private String username;
    private String email;
    private List<String> roles = new ArrayList<>();
}
```

## Controller

```java
// Add Reference to another api end point
@GetMapping(value = "/users")
@PreAuthorize("hasAnyRole('ROLE_ADMIN')")
public List<UserInfo> getAllUser(){
    List<UserInfo> userInfoList  = userService.getUserList();
    for (UserInfo userInfo : userInfoList) {
        Link selfLink = WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(this.getClass()).getUserInfoById(userInfo.getId())).withRel("Self");
        userInfo.add(selfLink);
    }
    return userInfoList;
}

@GetMapping("/user/{id}")
@PreAuthorize("hasAnyRole('ROLE_ADMIN')")
public UserInfo getUserInfoById(@PathVariable Long id){
    UserInfo userInfo = userService.getUserInfoById(id);
    Link allLink = WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(this.getClass()).getAllUser()).withRel("All");
    userInfo.add(allLink);
    return userInfo;
}
```

## Result

![](/files/-MTs6VB1DomuId8t8OCA)

![](/files/-MTs6hg3tfS2yq-dJp18)

* The data format will be application/hal+json


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://petercheng7788.gitbook.io/developer-note/backend/spring/hateos.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
