spring mvc 集成 swagger-ui2

1·spring mvc 構建 swagger-ui


1·添加依賴html

<dependency>
   <groupId>com.google.guava</groupId>
   <artifactId>guava</artifactId>
   <version>27.0.1-jre</version>
</dependency>
<!--swagger加強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資源文件java

<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>

<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

3·攔截器放行swagger資源文件git

<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請求受權github

<security:intercept-url pattern="/swagger-resources/**" access="permitAll" />

<security:intercept-url pattern="/v2/**" access="permitAll" />

5·建立swagger的配置類web

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("基礎平臺 RESTful APIs")

                .description("基礎平臺 RESTful 風格的接口文檔,內容詳細,極大的減小了先後端的溝通成本,同時確保代碼與文檔保持高度一致,極大的減小維護文檔的時間。")

                .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配置類spring

<bean class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration" id="swagger2Config"/>

7·運行項目 訪問地址:ip/項目名/doc.htmlbootstrap

swagger經常使用註解:
@ApiIgnore 註解能夠忽略這個類、方法、參數
@Api(tags={"這裏寫controller的註釋"})後端

@Api(tags={"知識庫接口"})
@RestController
@RequestMapping
public class InfoController {

@ApiOperation(value = "方法描述")api

@ApiOperation(value = "獲取某法律法規詳情數據")
	@RequestMapping(value = "/info", method = RequestMethod.GET)
	public Info getInfoById(

@ApiParam(name="id",value = "方法參數描述")mvc

public Info getInfoById(
			@ApiParam(name="id",value = "法律法規的ID") @RequestParam("id") String id)

@ApiModelProperty(value="實體字段描述")

@ApiModelProperty(value="ID")
	private String id;

@ApiModel(value = "實體類描述")

@ApiModel(value = "info:法律法規實體")
public class Info
相關文章
相關標籤/搜索