2

我是 springfox 和 swagger2 的新手。我一直在尝试将 springfox/swagger2 与我的 Spring Boot 微服务集成以生成 API 文档。

我按照“ http://springfox.github.io/springfox/docs/snapshot/ ”站点中给出的步骤进行操作。但是我没有成功带来api文档页面。

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

我不确定在我的应用程序中配置 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));
	}
}

4

2 回答 2

0

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

http://localhost:8081/<your-context-root-here>/swagger-ui.html
于 2016-03-01T18:47:17.163 回答
0

确保你包括

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>swagger-ui</artifactId>
    <version>2.1.8-M1</version>
</dependency>
于 2016-05-07T21:34:28.797 回答