Skip to content

Commit e77a675

Browse files
committed
[ADD] Spring Boot Hateoas
1 parent ddf8a95 commit e77a675

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

SpringBootHateoas/src/main/java/com/example/Message.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,4 @@ public LocalDateTime getCreatedAt() {
4242
public void setCreatedAt(LocalDateTime createdAt) {
4343
this.createdAt = createdAt;
4444
}
45-
46-
public MessageResource toResource() {
47-
return new MessageResource(this);
48-
}
4945
}
Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,32 @@
11
package com.example;
22

33
import org.springframework.hateoas.Link;
4-
import org.springframework.hateoas.mvc.ControllerLinkBuilder;
54
import org.springframework.http.HttpHeaders;
6-
import org.springframework.http.HttpStatus;
75
import org.springframework.http.ResponseEntity;
86
import org.springframework.web.bind.annotation.GetMapping;
97
import org.springframework.web.bind.annotation.PathVariable;
108
import org.springframework.web.bind.annotation.RequestMapping;
119
import org.springframework.web.bind.annotation.RestController;
1210

11+
import java.net.URI;
12+
import java.net.URISyntaxException;
1313
import java.time.Clock;
1414
import java.time.LocalDateTime;
1515

16-
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
17-
1816
@RestController
1917
@RequestMapping("/message")
2018
public class MessageController {
2119

2220
@GetMapping("/{id}")
23-
public ResponseEntity<MessageResource> get(@PathVariable Long id) {
21+
public ResponseEntity<MessageResource> get(@PathVariable Long id) throws URISyntaxException {
2422
Message message = new Message(id, "hello", LocalDateTime.now(Clock.systemUTC()));
25-
MessageResource messageResource = message.toResource();
26-
27-
// 1)
28-
// Link link = new Link("http://localhost:8080/message/" + id);
29-
// 2)
30-
ControllerLinkBuilder linkBuilder = linkTo(MessageController.class).slash(message);
31-
Link link = linkBuilder.withSelfRel();
3223

33-
messageResource.add(link);
24+
MessageResourceAssembler assembler = new MessageResourceAssembler();
25+
MessageResource resource = assembler.toResource(message);
3426

3527
HttpHeaders headers = new HttpHeaders();
36-
headers.setLocation(linkBuilder.toUri());
37-
return new ResponseEntity<>(headers, HttpStatus.OK);
28+
headers.setLocation(new URI(resource.getLink(Link.REL_SELF).getHref()));
29+
return ResponseEntity.ok().headers(headers).body(resource);
3830
}
3931
}
4032

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.example;
2+
3+
import org.springframework.hateoas.mvc.ResourceAssemblerSupport;
4+
5+
public class MessageResourceAssembler extends ResourceAssemblerSupport<Message, MessageResource> {
6+
7+
public MessageResourceAssembler() {
8+
super(MessageController.class, MessageResource.class);
9+
}
10+
11+
@Override
12+
public MessageResource toResource(Message entity) {
13+
return createResourceWithId(entity.getId(), entity);
14+
}
15+
16+
@Override
17+
protected MessageResource instantiateResource(Message entity) {
18+
return new MessageResource(entity);
19+
}
20+
}

0 commit comments

Comments
 (0)