sada-dream / sada-dream_server_v2

개인간 해외 직구 중계 플랫폼 사다드림입니다.
MIT License
0 stars 0 forks source link

Spring Boot를 이용한 RESTful Web Services 개발 [섹션 4] - Spring Boot API 사용 #29

Open dongwooklee96 opened 3 years ago

dongwooklee96 commented 3 years ago

Level3 단계의 REST API 구현을 위한 HATEOAS 적용

image

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-hateoas</artifactId>
            <scope>test</scope>
</dependency>

image

    @GetMapping("/users/{id}")
    public Resource<User> retriveUser(@PathVariable int id) {
        User user = service.findOne(id);

        if (user == null) {
            throw new UserNotFoundException(String.format("ID[%s] not found", id));
        }

        // HATEOAS
        Resource<User> resource = new Resource<>(user);

        ControllerLinkBuilder linkTo = ControllerLinkBuilder.linkTo(
            ControllerLinkBuilder.methodOn(this.getClass()).retrieveAllUsers());
        resource.add(linkTo.withRel("all-users"));

        return resource;
    }
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;
...
    @GetMapping("/users/{id}")
    public Resource<User> retriveUser(@PathVariable int id) {
        User user = service.findOne(id);

        if (user == null) {
            throw new UserNotFoundException(String.format("ID[%s] not found", id));
        }

        // HATEOAS
        Resource<User> resource = new Resource<>(user);

        //    ControllerLinkBuilder linkTo = ControllerLinkBuilder.linkTo(
         //   ControllerLinkBuilder.methodOn(this.getClass()).retrieveAllUsers());

         ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveAllUsers());

        resource.add(linkTo.withRel("all-users"));

        return resource;
    }

image

dongwooklee96 commented 3 years ago

REST API Documentation을 위한 Swagger 사용


image

image

package com.example.restfulwebservice.config;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;

import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configurable
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2);
    }
}
dongwooklee96 commented 3 years ago

그리고 나서 다음과 같은 주소로 접속하면 스웨거 도큐먼트 화면이 출력 된다.

http://localhost:8088/swagger-ui/index.html

image

dongwooklee96 commented 3 years ago

http://localhost:8088/v2/api-docs

또한 다음 주소로 접속하면, 스웨거 문서를 수정할 수 있는 화면이 나타난다.

image

자세한 설명은 아래 링크를 참조한다

dongwooklee96 commented 3 years ago

REST API Monitoring을 위한 Actuator 설정

pom.xml 파일에 아래와 같은 내용을 추가한다.

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.4.5</version>
</dependency>

http://localhost:8088/acturator 위의 링크에 접속하면 아래와 같은 화면을 볼 수 있다.

image

image

dongwooklee96 commented 3 years ago

HAL Browser를 이용한 HATEOAS 기능 구현

image

dongwooklee96 commented 3 years ago

Spring Security를 이용한 인증 처리

image

image