RESTful API
스프링 부트를 활용한 RESTful
스파이더웹
2022. 9. 19. 10:41
728x90
반응형
HATEOAS - Hypermedia As The Engine Of Application State
REST Api를 사용하는 클라이언트가 전적으로 서버와 동적인 상호작용이 가능하도록 하는 것을 의미합니다. 이러한 방법은 클라이언트가 서버로부터 어떠한 요청을 할 때, 요청에 필요한 URI를 응답에 포함시켜 반환하는 것으로 가능하게 할 수 있습니다. 예를 들어, 사용자 정보를 생성(POST)하는 요청 이후, 이를 조회, 수정, 삭제할 때, 이러한 모든 동작을 URI를 이용해 동적으로 알려준다는 의미입니다.
spring 2.2이 이상인 경우
EntityModel
WebMvcLinkBuilder를 사용한다
이전의 생성해두었던 유저의 개별조회 메서드에 내용을 추가해보자
@GetMapping("/users/{id}")
public EntityModel<User>retrieveUser(@PathVariable int id){
User user = service.findOne(id);
if(user==null){
throw new UserNotFoundException(String.format("ID[%s]가 없습니다",id));
}
EntityModel<User> entityModel = EntityModel.of(user);
WebMvcLinkBuilder builder = WebMvcLinkBuilder.linkTo(
WebMvcLinkBuilder.methodOn(this.getClass()).retrieveAllUser());
entityModel.add(builder.withRel("all-users"));
return entityModel;
}
개발자가 만든 REST API의 도움말 페이지를 생성하는 방법을 알아보자
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
swagger 관련하여 의존성 추가를 해준다.
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2);
}
}
Bean을 등록한다
http://localhost:8088/swagger-ui/index.html#/ 이동할 경우 api docs가 확인된다
참조
https://www.inflearn.com/course/spring-boot-restful-web-services/dashboard
Spring Boot를 이용한 RESTful Web Services 개발 - 인프런 | 강의
이 강의는 Spring Boot를 이용해서 RESTful Web Services 애플리케이션을 개발하는 과정에 대해 학습하는 강의으로써, REST API 설계에 필요한 기본 지식에 대해 학습할 수 있습니다., - 강의 소개 | 인프런..
www.inflearn.com
728x90
반응형