반응형
Hateoas
Rest API를 사용하는 클라이언트가 전적으로 서버와 동적인 상호작용이 가능하도록 하는 것
클라이언트가 서버로 어떠한 요청을 할 때, 요청에 필요한 URL를 응답에 포함시켠 반환하게 한다
HateoasTest
pom.xml 에 라이브러리 추가
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-hateoas
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
UserDTO
public class UserDTO {
private int no;
private String id;
private String pwd;
private String name;
private java.util.Date enrollDate;
public UserDTO() {}
public UserDTO(int no, String id, String pwd, String name, Date enrollDate) {
super();
this.no = no;
this.id = id;
this.pwd = pwd;
this.name = name;
this.enrollDate = enrollDate;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public java.util.Date getEnrollDate() {
return enrollDate;
}
public void setEnrollDate(java.util.Date enrollDate) {
this.enrollDate = enrollDate;
}
@Override
public String toString() {
return "UserDTO [no=" + no + ", id=" + id + ", pwd=" + pwd + ", name=" + name + ", enrollDate=" + enrollDate
+ "]";
}
}
반응형
ResponseMessage
package com.greedy.api.section04.hateoas;
import java.util.Map;
public class ResponseMessage {
private int httpStatus;
private String message;
private Map<String, Object> results;
public ResponseMessage(int httpStatus, String message, Map<String, Object> results) {
super();
this.httpStatus = httpStatus;
this.message = message;
this.results = results;
}
public int getHttpStatus() {
return httpStatus;
}
public void setHttpStatus(int httpStatus) {
this.httpStatus = httpStatus;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Map<String, Object> getResults() {
return results;
}
public void setResults(Map<String, Object> results) {
this.results = results;
}
@Override
public String toString() {
return "ResponseMessage [httpStatus=" + httpStatus + ", message=" + message + ", results=" + results + "]";
}
}
HateoasTestController
@RestController
@RequestMapping("/hateoas")
public class HateoasTestController {
private List<UserDTO> users;
public HateoasTestController() {
users = new ArrayList<>();
users.add(new UserDTO(1, "user01", "pass01", "뽀로로", new java.util.Date()));
users.add(new UserDTO(2, "user02", "pass02", "짱구", new java.util.Date()));
users.add(new UserDTO(3, "user03", "pass03", "또치", new java.util.Date()));
}
@GetMapping("/users")
public ResponseEntity<ResponseMessage> findAllUsers(){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
/* List<UserDTO>를 EntityModel 안에 정의 된 UserDTO로 바꾸고
EntityModel로 감싸진 userDTO를 Stream을 사용하여 가공(userNo고 전체 유저에 대한 링크 생성)하고
List로 반환 받는다
*/
List<EntityModel<UserDTO>> userWithRel =
users.stream().map(user ->EntityModel.of(user,
linkTo(methodOn(HateoasTestController.class).findUserByNo(user.getNo())).withSelfRel(),
linkTo(methodOn(HateoasTestController.class).findAllUsers()).withRel("users")
)).collect(Collectors.toList());
Map<String, Object> responseMap = new HashMap<>();
responseMap.put("users", userWithRel);
ResponseMessage responseMessage = new ResponseMessage(200, "조회 성공", responseMap);
return new ResponseEntity<>(responseMessage, headers, HttpStatus.OK);
}
}
유저 목록 조회
반응형
'프로그래밍 > Spring & Spring boot' 카테고리의 다른 글
[Spring boot] RESTAPI _ 05 Swagger (0) | 2022.11.03 |
---|---|
[Spring boot] RESTAPI _ 04 Exception & validation(데이터 검증) (0) | 2022.11.03 |
[Springboot] RESTAPI_03 ResponseEntity (0) | 2022.11.03 |
[Spring boot] RESTAPI_02 Response (0) | 2022.11.03 |
[Spring boot ] REST API - 01 REST API & POSTMAN (0) | 2022.11.02 |