如果你是一名Java開發(fā)工程師,那么使用swagger-bootstrap-ui將會非常簡單,只需要在原使用的基礎(chǔ)上,添加swagger-bootstrap-ui的maven引用jar包即可
由于是springfox-swagger
的增強(qiáng)UI包,所以基礎(chǔ)功能依然依賴Swagger,springfox-swagger
的jar包必須引入
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
然后引入SwaggerBootstrapUi的jar包
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>${lastVersion}</version>
</dependency>
Swagger2Config配置文件如下:
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.bycdao.cloud"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("swagger-bootstrap-ui RESTful APIs")
.description("swagger-bootstrap-ui")
.termsOfServiceUrl("http://localhost:8999/")
.contact("developer@mail.com")
.version("1.0")
.build();
}
}
swagger-bootstrap-ui默認(rèn)訪問地址是:http://${host}:${port}/doc.html
Springfox-swagger默認(rèn)提供了兩個(gè)Swagger接口,需要開發(fā)者放開權(quán)限(如果使用shiro權(quán)限控制框架等),如果使用SwaggerBootstrapUi的增強(qiáng)功能,還需放開增強(qiáng)接口地址,所以,放開的權(quán)限接口包括3個(gè),分別是:
Shiro
的相關(guān)配置實(shí)例如下:
<!---other settings-->
<property name="filterChainDefinitions">
<value>
/swagger-resources = anon
/v2/api-docs = anon
/v2/api-docs-ext = anon
/doc.html = anon
/webjars/** = anon
//others....
</value>
</property>
SpringBoot中訪問doc.html
報(bào)404的解決辦法
實(shí)現(xiàn)SpringBoot的WebMvcConfigurer
接口,添加相關(guān)的ResourceHandler
,代碼如下:
@SpringBootApplication
@ConditionalOnClass(SpringfoxWebMvcConfiguration.class)
public class SwaggerBootstrapUiDemoApplication implements WebMvcConfigurer{
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
使用SpringMvc的朋友.在web.xml中配置了DispatcherServlet
,則需要追加一個(gè)url匹配規(guī)則,如下
<servlet>
<servlet-name>cmsMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--默認(rèn)配置,.htm|.do|.json等等配置-->
<servlet-mapping>
<servlet-name>cmsMvc</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<!-- 配置swagger-bootstrap-ui的url請求路徑-->
<servlet-mapping>
<servlet-name>cmsMvc</servlet-name>
<url-pattern>/v2/api-docs</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>cmsMvc</servlet-name>
<url-pattern>/swagger-resources</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>cmsMvc</servlet-name>
<url-pattern>/v2/api-docs-ext</url-pattern>
</servlet-mapping>
如果使用的是其他語言的話,想使用swagger-bootstrap-ui,建議使用她的前端分離版本swagger-bootstrap-ui-front
,具體可參考前端分離的文檔說明
更多建議: