2023.09.13 - API를 빠르게 테스트하기

mjjin·2023년 9월 12일
0

build.gradle 의존성 추가

// Spring Data REST - 스프링 데이터 레포지토리를 자동으로 RESTful 웹 서비스로 노출
implementation 'org.springframework.boot:spring-boot-starter-data-rest'

// REST API를 탐색하고, 테스트 할 수 있는 웹 기반 인터페이스를 제공
implementation 'org.springframework.data:spring-data-rest-hal-explorer'

application 옵션 추가하기

// yaml
  data:
    rest:
      detection-strategy: annotated
      base-path: /api
// properties
data.rest.detection-strategy=annotated
data.rest.base-path=/api

detection-strategy = annotated : @RepositoryRestResource 어노테이션이 붙은 Repository만 REST 리소스로 노출시켜준다.

Repository 설정

@RepositoryRestResource
public interface ArticleRepository extends JpaRepository<Article, Long> {
}

위에 설정해두었던 대로, REST 리소스를 노출할 레포지토리에 @RepositoryRestResource를 달아준다.

실행

브라우저에 base-path를 지정해 준 경로대로 입력한다.
ex ) http://localhost:8080/api

컨트롤러를 아직 만들지 않았음에도 불구하고
HTTP Request 아래의 버튼들을 눌러
GET, POST 등을 편리하게 사용 할 수 있다.

10개의 댓글

comment-user-thumbnail
2023년 9월 17일

잘 보고 갑니다~

2개의 답글
comment-user-thumbnail
2023년 9월 17일

누구세용?

2개의 답글