추가정보를 전송하기 위한 hateoas 적용

This commit is contained in:
haerong22
2020-11-28 15:30:02 +09:00
parent 9e97a3b269
commit 650ea60f53
3 changed files with 16 additions and 4 deletions

View File

@@ -31,7 +31,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@@ -14,7 +14,7 @@ import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonFilter("UserInfo")
//@JsonFilter("UserInfo")
public class User {
private Integer id;
@Size(min = 2, message = "Name은 2글자 이상 입력해 주세요.")

View File

@@ -2,6 +2,8 @@ package com.example.restfulwebservice.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
@@ -11,6 +13,9 @@ import java.net.URI;
import java.util.List;
import java.util.Locale;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
@RestController
public class UserController {
private final UserDaoService userDaoService;
@@ -27,14 +32,18 @@ public class UserController {
}
@GetMapping("/users/{id}")
public User retrieveUser(@PathVariable int id) {
public EntityModel<User> retrieveUser(@PathVariable int id) {
User user = userDaoService.findOne(id);
if (user == null) {
throw new UserNotFoundException(String.format("ID[%s] not found", id));
}
return user;
EntityModel<User> entityModel = new EntityModel<>(user);
WebMvcLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveAllUsers());
entityModel.add(linkTo.withRel("all-users"));
return entityModel;
}
@PostMapping("/users")