강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

SOOMIN님의 프로필 이미지
SOOMIN

작성한 질문수

Spring Cloud로 개발하는 마이크로서비스 애플리케이션(MSA)

Webflux를 위한 Spring Cloud Gateway

api gateway 를 swagger 에 연동

작성

·

2.2K

0

api gateway 를 swagger 에 연동하는 방법 알려주시면 감사하겠습니다ㅜㅜ

답변 2

0

SOOMIN님의 프로필 이미지
SOOMIN
질문자

말씀해주신대로 pom dependency 와 SwaggerConfig 파일을 추가하였는데 위와 같이 에러가 납니다...

0

Dowon Lee님의 프로필 이미지
Dowon Lee
지식공유자

안녕하세요, 이도원입니다. 

apigateway-service에 swagger를 적용하시는 걸 말씀하시는 건가요? pom.xml과 SwaggerConfig.java 파일을 추가하시면 될 것 같습니다. 다만 apigateway-service에는 Controller를 별도로 추가하지 않은 상태로 어떤 actuator에 대한 기본적인 Endpoints가 확인 될 것 같습니다.

<dependency>
<groupId>io.springfox</groupId>
<!-- <artifactId>springfox-swagger2</artifactId>-->
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<!-- <version>2.9.2</version>-->
<version>3.0.0</version>
</dependency>
package com.example.apigatewayservice.config;

import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.actuate.endpoint.web.*;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.*;

//@Configuration
@EnableSwagger2
public class SwaggerConfig {
private static final Contact DEFAULT_CONTACT = new Contact("Kenneth Lee",
"http://www.joneculsting.co.kr", "edowon@joneconsluting.co.kr");
private static final ApiInfo DEFAULT_API_INFO = new ApiInfo("Awesome API Title",
"Awesome API Documentation", "1.0", "urn:tos",
DEFAULT_CONTACT, "Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
private static final Set<String> DEFAULT_PRODUCES_AND_CONSUMES = new HashSet<>(Arrays.asList(
"application/json", "application/xml"));

@Bean
public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,
ServletEndpointsSupplier servletEndpointsSupplier,
ControllerEndpointsSupplier controllerEndpointsSupplier,
EndpointMediaTypes endpointMediaTypes,
CorsEndpointProperties corsProperties,
WebEndpointProperties webEndpointProperties,
Environment environment) {
List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();
Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
allEndpoints.addAll(webEndpoints);
allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
String basePath = webEndpointProperties.getBasePath();
EndpointMapping endpointMapping = new EndpointMapping(basePath);
boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes,
corsProperties.toCorsConfiguration(),
new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping);
}

private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
}

@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(DEFAULT_API_INFO)
.produces(DEFAULT_PRODUCES_AND_CONSUMES)
.consumes(DEFAULT_PRODUCES_AND_CONSUMES)
;
}

// @Bean
// public Docket api() {
// return new Docket(DocumentationType.SWAGGER_2)
// .select()
// .apis(RequestHandlerSelectors.any())
// .paths(PathSelectors.any())
// .build();
// }
}

감사합니다. 

SOOMIN님의 프로필 이미지
SOOMIN
질문자

답변 감사합니다!!!

따라해보니까 아래와 같이 에러가 납니다ㅜㅜ

SOOMIN님의 프로필 이미지
SOOMIN

작성한 질문수

질문하기