无法通过 Spring Boot 获得 Swagger UI [英] Not able to get Swagger UI with spring boot
问题描述
我正在关注 http://raibledesigns.com/rd/entry/documenting_your_spring_api_with 上的文章.
I am following the article at http://raibledesigns.com/rd/entry/documenting_your_spring_api_with.
一切正常,但无法集成 Swagger UI.
Everything works fine but not able to integrate Swagger UI.
http://localhost:8080/docs/index.html
导致/error 重定向.
results in /error redirection.
推荐答案
我在基于 gradle 的 Spring Boot 应用程序中使用 swagger2 配置来回答这个问题.以下是 Swagger2 所需的配置.
I'm answering this with swagger2 configuration inside a gradle based spring boot application. Following are the configuration required for Swagger2.
添加 Gradle 配置
在 build.gradle 文件中添加 Gradle 依赖
Add Gradle dependencies inside build.gradle file
dependencies {
compile("io.springfox:springfox-swagger2:2.0.2")
compile("io.springfox:springfox-swagger-ui:2.0.2")
}
Swagger2 配置类
@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 界面
从 github 下载 Swagger UI.将 dist 文件夹复制到您的 src/main/resources/static 目录并将 dist 重命名为 swagger
Download the Swagger UI from github. Copy the dist folder into your src/main/resources/static directory and rename dist to 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 Boot 应用程序并运行以查看 API.
Configure your spring boot application with dependencies and run to see the API.
网址将是 http://localhost:8080/v2/swagger-ui.html您也可以按照上述答案进行自定义.
the url will be http://localhost:8080/v2/swagger-ui.html you can also customize this as above answer.
这篇关于无法通过 Spring Boot 获得 Swagger UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!