我对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));
}
}
是否包含应用程序的上下文根?
http://localhost:8081/<your-context-root-here>/swagger-ui.html
我希望文档的显示方式类似于它在特定于应用程序的上下文路径下的文档中提到的方式。你能告诉我我错过了什么吗?
我使用springboot和jersey作为restful API。现在我想将swagger2集成到我的项目中,但它不起作用。当我运行我的应用程序并访问http://localhost:8080/swagger-ui.html.我得到了swagger网页,但没有显示api(见下图)。似乎swagger没有找到我的api类。 下面是我添加的依赖项。 以下是我的申请课程: 下面是我的jersey配置类
我如何访问生成的swagger-ui.html?或者index.html我似乎在我的项目中找不到这个文件。啊啊!
我尝试使用@api和@apimodel来重命名我的api引用,但似乎什么都不起作用。它总是使用类名。Im使用swagger2+springfox 2.4+Springboot。
我正在使用springfox swagger2,它工作正常。 这只是一个基本的设置/配置,因为我对Swagger真的很陌生。 更新 如果在类中设置这个,我就可以访问。 但是如果我把它更改为,它就不会了,而且我会得到我设置的401错误: 依赖性(pom.xml): 安全配置类 MyAuthenticationEntryPoint 谢了!