我正在关注http://raibledesigns.com/rd/entry/documenting_your_spring_api_with上的文章。
一切正常但无法集成Swagger UI。
http://localhost:8080/docs/index.html
导致/错误重定向。
答案 0 :(得分:2)
我知道这是一个老问题,但也许这将有助于将来遇到类似问题的人。
我遵循了你提到的那个类似的教程,我让它没有问题。我在几周前的Spring启动项目中提供了自己的文档,介绍如何使用UI设置Swagger。也许它会帮助你,因为它更短,更新。
添加Maven依赖项
将它们粘贴在你的pom.xml中:
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>1.0.2</version>
<type>jar</type>
</dependency>
添加Swagger UI
从github下载Swagger UI。将dist
文件夹复制到webapp
目录,然后将dist
重命名为swagger
(或您喜欢的任何名称)。
打开复制目录中的 index.html 文件并更改第一个javascript函数中的网址,使其指向/api-docs endpoint
:
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
url = "/project-name/api-docs";
}
配置Swagger
创建一个 SwaggerConfig.java 类并在那里配置swagger:
@Configuration
@EnableSwagger
@EnableAutoConfiguration
public class SwaggerConfig {
private SpringSwaggerConfig springSwaggerConfig;
@Autowired
public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {
this.springSwaggerConfig = springSwaggerConfig;
}
@Bean
public SwaggerSpringMvcPlugin customImplementation() {
return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
// Root level documentation
.apiInfo(new ApiInfo("Swagger-demo JSON API", "This service provides a JSON representation the service API", null, null, null, null))
.useDefaultResponseMessages(false)
// Map the specific URL patterns into Swagger
.includePatterns("/greeting.*");
}
}
你的招摇应该现在正在运行。尝试访问/project-name/swagger/index.html
。
答案 1 :(得分:2)
我在基于gradle的spring boot应用程序中使用swagger2配置回答这个问题。以下是Swagger2所需的配置。
添加Gradle Configuartion
在build.gradle文件中添加Gradle依赖项
dependencies {
compile("io.springfox:springfox-swagger2:2.0.2")
compile("io.springfox:springfox-swagger-ui:2.0.2")
}
Swagger2 Confugration Class
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket userApi() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build().pathMapping("/")
.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()))
.apiInfo(apiInfo());
}
@Autowired
private TypeResolver typeResolver;
private ApiKey apiKey() {
return new ApiKey("mykey", "api_key", "header");
}
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));
}
@Bean
SecurityConfiguration security() {
return new SecurityConfiguration("123456", "test-app-realm", "clientapp", "apiKey");
}
@Bean
UiConfiguration uiConfig() {
return new UiConfiguration("validatorUrl");
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo("DSM API", "API for DSM", "1.0.0", "termsOfServiceUrl",
"nijo@ndimensionz.com", null, null);
return apiInfo;
}
}
添加Swagger UI
从github下载Swagger UI。将dist文件夹复制到 src / main / resources / static 目录中,并将dist重命名为swagger
<强> HomeController.class 强>
@Api(basePath = "/", value = "/", description = "Home Controller")
@Controller
public class HomeController {
@RequestMapping("/")
public String home() {
return "redirect:swagger-ui.html";
}
}
<强> MyApplication.class 强>
@SpringBootApplication
@ComponentScan(basePackageClasses = SwaggerConfig.class)
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
使用依赖项配置Spring启动应用程序并运行以查看API。
网址为http://localhost:8080/v2/swagger-ui.html 您也可以按上述答案自定义。
答案 2 :(得分:0)
我也面临同样的问题。在添加dest文件夹之后,我能够看到json但不会招摇ui。在初始化类扩展 SpringBootServletInitializer 我添加了下面的方法,然后它工作正常
<td> TREASURE <span>TREASURE</span> </td> -> YES
请参阅link
答案 3 :(得分:0)
这是我的工作配置,以防其他人
@Configuration
@EnableSwagger2
@Profile({"!production"})
public class SwaggerConfiguration extends WebMvcConfigurerAdapter {
@Autowired
private ServletContext servletContext;
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.host("localhost")
.directModelSubstitute(LocalDate.class, Date.class)
.pathProvider(new RelativePathProvider(servletContext) {
@Override
public String getApplicationBasePath() {
return "/docs";
}
})
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
由于我也在使用Spring安全性,因此我还必须将以下内容添加到白名单中:
private String[] swaggerWhiteList = {"/v2/api-docs", "/swagger-resources/configuration/ui", "/swagger-resources", "/swagger-resources/configuration/security", "/swagger-ui.html", "/swagger.json", "/webjars/**"};
然后我可以使用以下网址访问它: