Springboot 4.Springboot 集成SwaggerUi


SwaggerUi就是自動生成接口文檔的這么一個類似於插件的工具,可以直接訪問接口。

首先打開pom文件,將插件引進來,然后增加一個屬性<properties>,用來設置版本號的,然后直接用${}引用。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>JavaInterfaceTest</artifactId>
        <groupId>com.peixm.code</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Chapter10</artifactId>

    <properties> <swagger.version>2.6.1</swagger.version> </properties> 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger.version}</version> </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.14</version>
        </dependency>
    </dependencies>

</project>

然后創建一個config包,在創建一個類SwaggerConfig.java,用來配置swager

package com.course.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration  //在springboot里面專門為了加載配置文件的標簽
@EnableSwagger2   //自動加載配置文件
public class SwaggerConfig {
    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .pathMapping("/")
                .select()
                .paths(PathSelectors.regex("/.*"))   //匹配那些訪問的方法
                .build();
    }

    private ApiInfo apiInfo() {
        //http://localhost:8888/swagger-ui.html
        return new ApiInfoBuilder().title("我的接口文檔")
                .contact(new Contact("xiaomin","","553238711@qq.com"))
                .description("這是我的swaggerui生成的接口文檔")
                .version("1.0.0.0")
                .build();
    }

}

然后在想要在swagger看到的接口類的類名上添加注解:@Api(value = "/",description = "這是我全部的get方法"),在每個方法上面添加  @ApiOperation(value = "通過這個方法可以獲取到cookies的值",httpMethod ="GET"),(或者post)value就是一個描述,描述這個方法是做什么的。

package com.course.server;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController    //被告訴我是你需要掃描的類
@Api(value = "/",description = "這是我全部的get方法")
public class MyGetMethod {

    @RequestMapping(value = "/getCookies",method = RequestMethod.GET)    //訪問的路徑是什么
    @ApiOperation(value = "通過這個方法可以獲取到cookies的值",httpMethod ="GET")
    public String getCookies(HttpServletResponse response){
        //HttpServerletRequest    裝請求信息
        //HttpServerletResponse   裝響應信息
        Cookie cookie = new Cookie("login","ture");
        response.addCookie(cookie);

        return "恭喜你獲得cookies信息成功";
    }

    /**
     * 要求客戶端攜帶cookies訪問
     * */

    @RequestMapping(value = "/get/with/cookies",method = RequestMethod.GET)
    @ApiOperation(value = "要求客戶端攜帶cookies訪問",httpMethod = "GET")
    public String getWithCookies(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if(Objects.isNull(cookies)){
            return "你必須攜帶cookies信息來";
        }

        for(Cookie cookie : cookies){
            if(cookie.getName().equals("login") && cookie.getName().equals("true")){
                return "恭喜你訪問成功";
            }
        }
        return "你必須攜帶cookies信息來";
    }

    /**
    * 開發一個需要攜帶參數才能訪問的get請求
    * 第一種實現方式是 url: ip:port/get/with/param?key=value&key=value
    * 模擬獲取商品列表  開始頁數,結束的頁數,一頁20條數據
    * */

    //第一種需要攜帶參數訪問的get請求
    @RequestMapping(value = "/get/with/param",method = RequestMethod.GET)
    @ApiOperation(value = "攜帶參數才能訪問的get請求",httpMethod = "GET")
    public Map<String,Integer> getList(@RequestParam Integer start,
                                       @RequestParam Integer end){
        Map<String,Integer> myList = new HashMap<>();
        myList.put("鞋",400);
        myList.put("襯衫",300);
        myList.put("干脆面",1);

        return myList;

    }

    /**
     *第2種需要攜帶參數訪問的get請求
     * url:  ip:port/get/with/param/10/20
     * */

    @RequestMapping(value = "/get/with/param/{start}/{end}")
    @ApiOperation(value = "第2種需要攜帶參數訪問的get請求",httpMethod = "GET")
    public Map myGetList(@PathVariable Integer start,
                         @PathVariable Integer end){

        Map<String,Integer> myList = new HashMap<>();
        myList.put("鞋",400);
        myList.put("襯衫",300);
        myList.put("干脆面",1);

        return myList;
    }

}

然后改變啟動文件里面的要檢測的包

然后在瀏覽器輸入:http://localhost:8888/swagger-ui.html 就會出現所有的接口

點擊接口可以進行接口測試:try out就可以請求

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM