Last active
November 1, 2023 07:12
-
-
Save mvitz/05a822b74cb62bf51ae8793ed0e58a93 to your computer and use it in GitHub Desktop.
Spring-Boot HATEOAS server example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>1.4.3.RELEASE</version> | |
</parent> | |
<groupId>de.mvitz</groupId> | |
<artifactId>spring-hateoas-server</artifactId> | |
<version>0.1.0-SNAPSHOT</version> | |
<properties> | |
<java.version>1.8</java.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-hateoas</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-devtools</artifactId> | |
<optional>true</optional> | |
</dependency> | |
</dependencies> | |
<build> | |
<finalName>${project.artifactId}</finalName> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~/Development/new/spring-hateoas-server $ http -p Hhb :8080/users Accept:application/hal+json | |
GET /users HTTP/1.1 | |
Accept: application/hal+json | |
Accept-Encoding: gzip, deflate | |
Connection: keep-alive | |
Host: localhost:8080 | |
User-Agent: HTTPie/0.9.8 | |
HTTP/1.1 200 | |
Content-Type: application/hal+json;charset=UTF-8 | |
Date: Wed, 18 Jan 2017 10:52:51 GMT | |
Transfer-Encoding: chunked | |
[ | |
{ | |
"links": [ | |
{ | |
"href": "http://localhost:8080/users/foo", | |
"rel": "self" | |
} | |
] | |
} | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~/Development/new/spring-hateoas-server $ http -p Hhb :8080/users | |
GET /users HTTP/1.1 | |
Accept: */* | |
Accept-Encoding: gzip, deflate | |
Connection: keep-alive | |
Host: localhost:8080 | |
User-Agent: HTTPie/0.9.8 | |
HTTP/1.1 200 | |
Content-Type: application/json;charset=UTF-8 | |
Date: Wed, 18 Jan 2017 10:20:50 GMT | |
Transfer-Encoding: chunked | |
[ | |
{ | |
"_links": { | |
"self": { | |
"href": "http://localhost:8080/users/foo" | |
} | |
} | |
} | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~/Development/new/spring-hateoas-server $ http -p Hhb :8080/users | |
GET /users HTTP/1.1 | |
Accept: */* | |
Accept-Encoding: gzip, deflate | |
Connection: keep-alive | |
Host: localhost:8080 | |
User-Agent: HTTPie/0.9.8 | |
HTTP/1.1 200 | |
Content-Type: application/json;charset=UTF-8 | |
Date: Wed, 18 Jan 2017 10:20:26 GMT | |
Transfer-Encoding: chunked | |
[ | |
{ | |
"links": [ | |
{ | |
"href": "http://localhost:8080/users/foo", | |
"rel": "self" | |
} | |
] | |
} | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.mvitz.spring.hateoas.server; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.hateoas.ExposesResourceFor; | |
import org.springframework.hateoas.ResourceSupport; | |
import org.springframework.hateoas.config.EnableHypermediaSupport; | |
import org.springframework.hateoas.mvc.ResourceAssemblerSupport; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.ResponseBody; | |
import java.util.Collections; | |
import java.util.List; | |
import static org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType.HAL; | |
@SpringBootApplication | |
//@EnableHypermediaSupport(type = HAL) | |
public class SpringHateoasApplication { | |
static class User { | |
String name; | |
User(String name) { this.name = name; } | |
} | |
static class UserResource extends ResourceSupport { | |
String name; | |
public UserResource(User user) { this.name = user.name; } | |
} | |
static class UserResourceAssembler extends ResourceAssemblerSupport<User, UserResource> { | |
public UserResourceAssembler() { | |
super(UserController.class, UserResource.class); | |
} | |
@Override | |
protected UserResource instantiateResource(User entity) { return new UserResource(entity); } | |
@Override | |
public UserResource toResource(User entity) { return createResourceWithId(entity.name, entity); } | |
} | |
@Controller | |
@RequestMapping("/users") | |
@ResponseBody | |
@ExposesResourceFor(UserResource.class) | |
class UserController { | |
UserResourceAssembler userResourceAssembler = new UserResourceAssembler(); | |
@RequestMapping | |
public List<UserResource> users() { | |
return Collections.singletonList(user("foo")); | |
} | |
@RequestMapping("/{name}") | |
public UserResource user(@PathVariable("name") String name) { | |
return userResourceAssembler.toResource(new User(name)); | |
} | |
} | |
public static void main(String[] args) { | |
SpringApplication.run(SpringHateoasApplication.class, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment