Rest API를 편리하게 문서화 해주고, 이를 통해서 관리 및 제 3의 사용자가 편리하게 API를 호출해보고 테스트 할 수 있는 프로젝트
Springfox와 Springdoc이 있는데, 최신 개발 환경에 더 적합한 것은 Springdoc이다. 그러므로, 실습에서도 SpringBoot 3.1.4 + Springdoc Swagger 환경을 사용할 것이다.
(SpringBoot의 버전이 2.x.x인 경우, 버전 충돌로 인해 Whitelabel Error가 발생할 수 있으므로, SpringBoot의 버전을 3.0.0 이상으로 변경할 것을 권장한다.)
//Swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'
Swagger-UI는 http://localhost:8080/swagger-ui/index.html 로 들어가서 확인
Swagger의 버전에따라 접속할 수 있는 URL 이 다르니 참고!
2.x.x 버전: localhost:8080/swagger-ui.html
3.x.x 버전: http://localhost:8080/swagger-ui/index.html

Username 에는 user , Password 는 실행하면 콘솔창에 뜬 비밀번호(아래 캡처 참고)를 입력하면 스웨거 페이지로 이동이 가능

package com.readmate.ReadMate.config;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf((csrfConfig) ->
csrfConfig.disable()
)
.headers((headerConfig)->headerConfig.frameOptions(frameOptionsConfig -> frameOptionsConfig.disable()))
.authorizeHttpRequests((authorizeRequests)-> authorizeRequests
.requestMatchers("/v2/api-docs", "/v3/api-docs", "/v3/api-docs/**", "/swagger-resources",
"/swagger-resources/**", "/configuration/ui", "/configuration/security", "/swagger-ui/**",
"/webjars/**", "/swagger-ui.html").permitAll()
.anyRequest().authenticated()
);
return http.build();
}
}
만일 이렇게 했을 때 스웨거가 아닌 Whitelabel Error Page 가 나온다면 참고 블로그 확인해보자
Spring Security 설정시 참고

package com.readmate.ReadMate.club.controller;
import com.readmate.ReadMate.common.exception.enums.ErrorCode;
import com.readmate.ReadMate.common.message.BasicResponse;
import com.readmate.ReadMate.common.message.ErrorResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/club")
@RequiredArgsConstructor
@Tag(name = "Book Club", description = "Book Club API")
public class ClubController {
@PostMapping()
@Operation(summary = "북클럽 생성", description = "북클럽 생성시 사용하는 API")
public ResponseEntity<BasicResponse<?>> testAPI() {
try {
// 성공적으로 API 호출될 때의 응답
return ResponseEntity.ok(BasicResponse.ofSuccess("API 호출 성공"));
} catch (Exception e) {
// 에러가 발생했을 때 에러 코드를 사용하여 처리
ErrorResponse response = new ErrorResponse(ErrorCode.INVALID_BOARD);
return new ResponseEntity<>(BasicResponse.of(
HttpStatus.NOT_FOUND, // ErrorCode의 상태 코드
response.getError(), // Error 메시지
response // Error 응답 본문
), HttpStatus.NOT_FOUND);
}
}
}
결론은, 배포후 공유해야한다!
@RestController
@RequestMapping("/")
@RequiredArgsConstructor
@Tag(name = "Response Estimate", description = "Response Estimate API")
public class ResponseEstimateController {
...
// 이와 같은 방식으로 모든 Controller에 @Tag를 적용한다.
@PostMapping("/signup")
@Operation(summary = "회원가입", description = "회원가입 할 때 사용하는 API")
public BaseResponse<?> signUp(@RequestBody SignupReq request) {
try {
return new BaseResponse<>(companyService.signup(request));
} catch (BaseException exception) {
return new BaseResponse<>(exception.getStatus());
}
}
@ApiResponse 어노테이션은 API 응답에 대한 설명과 상태 코드를 정의하는 데 사용된다. @Operation과 마찬가지로 클래스 내부 메서드에 사용
@Schema 어노테이션은 API 모델의 속성을 정의하고 문서화하는 데 사용된다. 다시 말해서 요청과 응답에 사용되는 DTO 클래스나 필드에 사용할 수 있다.