SpringBoot集成SwaggerUI,步驟如下:
1.打開idea配置pom文件
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.course.code</groupId>
<artifactId>Chapter10</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
</project>
2.創建配置類
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
@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(){
return new ApiInfoBuilder().title("我的接口文檔")
.contact(new Contact("lxy","","1140553684"))
.description("這是我的swaggerui生成的接口文檔")
.version("1.0.0.0")
.build();
}
}
3.創建接口類
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){
//HttpServletResquest 裝請求信息的類
//HttpServletResponse 裝響應信息的類
Cookie cookie=new Cookie("login","true");
response.addCookie(cookie);
return "恭喜你獲得cookies信息成功";
}
/**
* 要求客戶端攜帶cookies訪問
*/
@RequestMapping(value = "/get/with/cookies",method = RequestMethod.GET)
@ApiOperation(value = "這是一個需要攜帶cookies信息才能訪問的get請求",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.getValue().equals("true")){
return "這是一個需要攜帶cookies信息才能訪問的get請求!";
}
}
return "你必須攜帶cookies信息來";
}
/**
* 開發一個需要攜帶參數才能訪問的get請求
* 第一種實現方式 URL:key=value&key=value
* 模擬獲取商品列表
*/
@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("shoes",400);
myList.put("乾脆面",1);
myList.put("襯衫",300);
return myList;
}
/**
* 第二種需要攜帶參數訪問的get請求
* URL:IP:port/get/with/param/10/20
*/
@RequestMapping(value = "/get/with/param/{start}/{end}")
@ApiOperation(value = "第一種方式帶參數的get請求",httpMethod = "GET")
public Map myGetList(@PathVariable Integer start,@PathVariable Integer end){
Map<String ,Integer> myList = new HashMap<>();
myList.put("shoes",400);
myList.put("pants",300);
return myList;
}
}
4.創建入口Application類
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.course")
public class Application {
public static void main(String args[]){
SpringApplication.run(Application.class,args);
}
}
運行入口類,然後瀏覽器中輸入網址http://localhost:8080/swagger-ui.html即可訪問