@Configuration
@Profile({"dev", "local", "mem"})
public class SwaggerConfig {
@Bean
public OpenApiCustomiser customOpenApi() {
return openApi -> {
String bearer = "bearer";
openApi
.info(new Info().title("instakyuram"))
.components(
openApi.getComponents().addSecuritySchemes(
bearer,
new SecurityScheme()
.type(SecurityScheme.Type.HTTP)
.scheme(bearer)
.bearerFormat("JWT")
.in(SecurityScheme.In.HEADER)
.name("Authorization")
)
)
.addSecurityItem(
new SecurityRequirement()
.addList(bearer, Arrays.asList("read", "write"))
);
};
}
}
spring fox와 달리 다른 config 설정을 해주지 않아도 됩니다.
기존의 yml 파일에 설정했던 저희 팀 swagger의 정보입니다. 이 부분은 위 코드의 .info(new Info().title("instakyuram)) 에 추가할 수 있습니다.
🫒 기존 코드와 변경된 부분 비교 해보기
기존의 코드(Spring fox)
@Api(tags= "팔로우, 팔로잉 api")
@RestController
@RequestMapping("/api/friendships")
public class FollowRestController {
private final FollowService followService;
public FollowRestController(FollowService followService) {
this.followService = followService;
}
@Operation(
summary = "팔로우 가능 여부 판단",
description = "팔로우 할 수 있는 대상인지 확인합니다.",
responses = {
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "팔로우 가능 여부 true/false"),
}
)
@GetMapping("/{memberId}")
public ApiResponse<Boolean> isFollowed(@AuthenticationPrincipal JwtAuthentication auth,
@PathVariable Long memberId) {
public ApiResponse<Boolean> isFollowed(
@AuthenticationPrincipal JwtAuthentication auth,
@Parameter(
name = "팔로우 할 대상의 식별 값", description = "사용자 id 값(숫자)을 입력합니다.", in = ParameterIn.PATH, required = true
) @PathVariable Long memberId) {
return new ApiResponse<>(followService.isFollowed(auth.id(), memberId));
}
spring doc 사용
@Tag(name = "팔로우, 팔로잉 api")
@RestController
@RequestMapping("/api/friendships")
public class FollowRestController {
private final FollowService followService;
public FollowRestController(FollowService followService) {
this.followService = followService;
}
@Operation(summary = "팔로우 가능 여부 판단", description = "팔로우 할 수 있는 대상인지 확인합니다.")
@GetMapping("/{memberId}")
public ApiResponse<Boolean> isFollowed(
@AuthenticationPrincipal JwtAuthentication auth,
@PathVariable Long memberId) {
return new ApiResponse<>(followService.isFollowed(auth.id(), memberId));
}