添加swagger

先後端分離開發時,前端如何得知後端的接口名稱以及參數,後端如何調試本身的端口是否正確是一個很嚴肅的話題,swagger爲這些問題提供了很大的便利html

一、在pom文件加依賴前端

<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.9.2</version>
</dependency>
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.9.2</version>
</dependency>java

二、新建一個 java 類 spring

@Bean
    public Docket buildDocket() {
        return  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())//調用下面apiInfo()方法
                .select()
                //Controller所在路徑
                .apis(RequestHandlerSelectors.basePackage("com.unicom.news.controller"))  // controller 所在包名
                .paths(PathSelectors.any())
                .build();
      }
 
    public ApiInfo apiInfo() {
            return  new ApiInfoBuilder()
                    .title("springboot結合swagger2構建Restful API")
                    .description("這是一個swagger2小型demo")
                    .termsOfServiceUrl("localhost:8080")
                    .version("0.0.1")
                    .build();
 
     }後端

三、啓動項目後,打開瀏覽器,在瀏覽器輸入http://localhost:8080/swagger-ui.html 便可查看端口,以及端口的method,參數api

/**********************************   轉載請附上連接 https://www.cnblogs.com/renxq/p/11075272.html ***********************************/瀏覽器

相關文章
相關標籤/搜索