您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)spring mvc如何集成swagger-ui2,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
1·添加依賴
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>27.0.1-jre</version> </dependency> <!--swagger增強(qiáng)UI--> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.6</version> </dependency> <!-- swagger2 核心依賴 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
2`添加swagger資源文件
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/> <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
3·攔截器放行swagger資源文件
<mvc:exclude-mapping path="/swagger*/**"></mvc:exclude-mapping> <mvc:exclude-mapping path="/v2/**"></mvc:exclude-mapping> <mvc:exclude-mapping path="/webjars/**"></mvc:exclude-mapping>
4·swagger請求授權(quán)
<security:intercept-url pattern="/swagger-resources/**" access="permitAll" /> <security:intercept-url pattern="/v2/**" access="permitAll" />
5·創(chuàng)建swagger的配置類
package swagger; import io.swagger.annotations.ApiOperation; import org.springframework.context.annotation.Bean; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; //啟用Swagger2 @EnableSwagger2 public class Swagger2Config extends WebMvcConfigurationSupport { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()).select() //掃描指定包中的swagger注解 //.apis(RequestHandlerSelectors.basePackage("com.xia.controller")) //掃描所有有注解的api,用這種方式更靈活 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build(); } @Bean private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("基礎(chǔ)平臺(tái) RESTful APIs") .description("基礎(chǔ)平臺(tái) RESTful 風(fēng)格的接口文檔,內(nèi)容詳細(xì),極大的減少了前后端的溝通成本,同時(shí)確保代碼與文檔保持高度一致,極大的減少維護(hù)文檔的時(shí)間。") .termsOfServiceUrl("http://xiachengwei5.coding.me") .version("1.0.0") .termsOfServiceUrl("http://xxx.xxx.com") .license("LICENSE") .licenseUrl("http://xxx.xxx.com") .build(); } @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addRedirectViewController("/docApi/v2/api-docs", "/v2/api-docs"); registry.addRedirectViewController("/docApi/swagger-resources/configuration/ui", "/swagger-resources/configuration/ui"); registry.addRedirectViewController("/docApi/swagger-resources/configuration/security", "/swagger-resources/configuration/security"); registry.addRedirectViewController("/docApi/swagger-resources", "/swagger-resources"); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/docApi/swagger-ui.html**").addResourceLocations("classpath:/META-INF/resources/swagger-ui.html"); registry.addResourceHandler("/docApi/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } }
6·加載swagger配置類
<bean class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration" id="swagger2Config"/>
7·運(yùn)行項(xiàng)目 訪問地址:ip/項(xiàng)目名/doc.html
swagger常用注解:
@ApiIgnore 注解可以忽略這個(gè)類、方法、參數(shù)
@Api(tags={"這里寫controller的注釋"})
@Api(tags={"知識(shí)庫接口"}) @RestController @RequestMapping public class InfoController {
@ApiOperation(value = "方法描述")
@ApiOperation(value = "獲取某法律法規(guī)詳情數(shù)據(jù)") @RequestMapping(value = "/info", method = RequestMethod.GET) public Info getInfoById(
@ApiParam(name="id",value = "方法參數(shù)描述")
public Info getInfoById( @ApiParam(name="id",value = "法律法規(guī)的ID") @RequestParam("id") String id)
@ApiModelProperty(value="實(shí)體字段描述")
@ApiModelProperty(value="ID") private String id;
@ApiModel(value = "實(shí)體類描述")
@ApiModel(value = "info:法律法規(guī)實(shí)體") public class Info
以上就是spring mvc如何集成swagger-ui2,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。