当前位置: 首页 > 知识库问答 >
问题:

SpringFox/Swagger2与springboot应用程序的集成

曾沛
2023-03-14

我对springfox和Swagger2都是新手。我一直在尝试将SpringFox/Swagger2与我的spring boot微服务集成以生成API文档。

我遵循了“http://springfox.github.io/springfox/docs/snapshot/”站点中给出的步骤。但是我没有成功的带来api文档页面。

每当我试图点击URL“http://localhost:8081/swagger-ui.html”时,我就会在浏览器“whitelabel error page”中看到一个通用错误页面。

我不确定我在应用程序中配置springfox时犯了什么错误。

package com.diginsite.microservices;

import static com.google.common.base.Predicates.or;
import static com.google.common.collect.Lists.newArrayList;
import static springfox.documentation.builders.PathSelectors.regex;
import static springfox.documentation.schema.AlternateTypeRules.newRule;

import java.util.List;

import org.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.schema.WildcardType;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import com.fasterxml.classmate.TypeResolver;
import com.google.common.base.Predicate;

//@SpringBootApplication
@EnableSwagger2
@Configuration
//@EnableWebMvc
@ComponentScan("com.diginsite.microservices.web")
public class Swagger2SpringBoot {
	static final String detailDescription = "The `Business Banking Microservice` is a RESTful API that provides PD teams with out of the box functionality for Digital Insight's `Business Banking` suite of products. \n \n"
			+"Below is a list of available REST API calls for business banking resources.";
	@Autowired
	private TypeResolver typeResolver;
	
//	public static void main(String[] args) {
//        ApplicationContext ctx = SpringApplication.run(Swagger2SpringBoot.class, args);
//    }

	@Bean
	public Docket swaggerSpringMvcPlugin() {
		return new Docket(DocumentationType.SWAGGER_2)
				.groupName("v1-bbs")
				.apiInfo(apiInfo())
				.select()
				.apis(RequestHandlerSelectors.any())
				.paths(PathSelectors.any())
				.build()
				.pathMapping("/v1")
				.directModelSubstitute(LocalDate.class, String.class)
				.genericModelSubstitutes(ResponseEntity.class)
				.alternateTypeRules(newRule(typeResolver.resolve(DeferredResult.class,
								typeResolver.resolve(ResponseEntity.class,
										WildcardType.class)), typeResolver
								.resolve(WildcardType.class)))
				.useDefaultResponseMessages(false)
				.globalResponseMessage(RequestMethod.GET,
						newArrayList(new ResponseMessageBuilder().code(500)
								.message("500 message")
								.responseModel(new ModelRef("Error")).build()))
				.securitySchemes(newArrayList(apiKey()))
				.securityContexts(newArrayList(securityContext()));
	}

	private Predicate<String> entitlementsAPIPaths() {
        return or(
                regex("/v1/fis/{fiId}/businessCustomers.*"),
                regex("/v1/fis/{fiId}/companies/{companyId}/entitlements.*")
        );
    }
	
	private ApiKey apiKey() {
		return new ApiKey("mykey", "api_key", "header");
	}
	
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Overview")
                .description(detailDescription)
                .termsOfServiceUrl("http://springfox.io")
                .contact("springfox")
                .license("Apache License Version 2.0")
                .licenseUrl("https://github.com/springfox/springfox/blob/master/LICENSE")
                .version("2.0")
                .build();
    }


	private SecurityContext securityContext() {
		return SecurityContext.builder().securityReferences(defaultAuth())
				.forPaths(PathSelectors.regex("/anyPath.*")).build();
	}

	List<SecurityReference> defaultAuth() {
		AuthorizationScope authorizationScope = new AuthorizationScope(
				"global", "accessEverything");
		AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
		authorizationScopes[0] = authorizationScope;
		return newArrayList(new SecurityReference("mykey", authorizationScopes));
	}
}

共有1个答案

云新知
2023-03-14

是否包含应用程序的上下文根?

http://localhost:8081/<your-context-root-here>/swagger-ui.html
 类似资料: