溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

spring mvc如何集成swagger-ui2

發布時間:2021-10-11 10:01:19 來源:億速云 閱讀:226 作者:柒染 欄目:大數據

本篇文章給大家分享的是有關spring mvc如何集成swagger-ui2,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

1·spring mvc 構建 swagger-ui

spring mvc如何集成swagger-ui2
spring mvc如何集成swagger-ui2

1·添加依賴

<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資源文件

<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請求授權

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

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

5·創建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("基礎平臺 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配置類

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

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

swagger常用注解:
@ApiIgnore 注解可以忽略這個類、方法、參數
@Api(tags={"這里寫controller的注釋"})

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

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

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

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

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

以上就是spring mvc如何集成swagger-ui2,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女