我正在关注http://raibledesigns.com/rd/entry/documenting_your_spring_api_with上的文章。
一切正常,但无法集成 Swagger UI。
http://localhost:8080/docs/index.html
导致 /error 重定向。
我正在关注http://raibledesigns.com/rd/entry/documenting_your_spring_api_with上的文章。
一切正常,但无法集成 Swagger UI。
http://localhost:8080/docs/index.html
导致 /error 重定向。
我在基于 gradle 的 Spring Boot 应用程序中使用 swagger2 配置来回答这个问题。以下是 Swagger2 所需的配置。
添加 Gradle 配置
在 build.gradle 文件中添加 Gradle 依赖项
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
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。
url 将是http://localhost:8080/v2/swagger-ui.html 您也可以按照上面的答案自定义它。
我知道这是一个老问题,但也许这会帮助将来遇到类似问题的人。
我按照与您提到的类似的教程进行操作,并且没有问题地使其工作。几周前,我在 Spring Boot 项目中发布了关于如何使用 UI 设置 Swagger 的文档。也许它会对您有所帮助,因为它更短且更新。
添加 Maven 依赖项
将这些粘贴在您的 pom.xml 中:
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>1.0.2</version>
<type>jar</type>
</dependency>
添加 Swagger 用户界面
从 github下载Swagger UI 。将dist
文件夹复制到您的webapp
目录并重命名dist
为swagger
(或您喜欢的任何名称)。
打开复制目录中的index.html文件并更改第一个 javascript 函数中的 url,使其指向/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
.
这是我的工作配置,以防它帮助其他人
@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/**"};
然后我可以使用以下网址访问它:
我也面临同样的问题。在添加 dest 文件夹后,我能够看到 json 但看不到 ui。在扩展SpringBootServletInitializer的初始化类中,我添加了下面的方法,然后它工作得很好
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SampleWebStaticApplication.class);
}
请参考链接