Spring項目中swagger用法與swagger-ui使用
一、swagger用法
1.1、編寫springboot項目

package com.xbmu.controller;
import com.xbmu.bean.Person;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping(value = "/person")
public class PersonController {
@PostMapping("/postReq")
public String postReq()
{
return "postReq";
}
@GetMapping("/getReq")
public String getReq(String param1,String param2)
{
return "getReq";
}
@RequestMapping("/req")
public String req(String param1)
{
return "req";
}
@RequestMapping(value = "/getPersonSingle",method = RequestMethod.GET)
private Person getPersonSingle(Integer id){
Person person;
List<Person> personList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
person = new Person();
person.setId(i+1);
person.setName("zhangsan"+(i+1));
person.setGender("男"+i);
person.setAge(18+1);
person.setAddress("shanxi xian");
personList.add(person);
}
person = personList.get(id);
return person;
}
}
1.2、導(dǎo)入spring-fox依賴
在項目pom.xml中導(dǎo)入spring-fox依賴,該項目選擇版本為2.9.2。其中springfox-swagger2是核心內(nèi)容的封裝。springfox-swagger-ui是對swagger-ui的封裝。
<?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.xbmu</groupId>
<artifactId>swagger-study</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<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.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
</project>
1.3、添加注解
在springboot的啟動類中添加 @EnableSwagger2 注解。
添加此注解后表示對當前項目中全部控制器進行掃描。應(yīng)用swagger2。
package com.xbmu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* EnableSwagger2 是springfox提供的一個注解,代表swagger2相關(guān)技術(shù)開啟。
* 會掃描當前類所在包,及子包中所有的類型中的注解。
*/
@SpringBootApplication
@EnableSwagger2
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class,args);
}
}
1.4、訪問swagger-ui
啟動項目后在瀏覽器中輸入 http://ip:port/swagger-ui.html 既可以訪問到swagger-ui頁面,在頁面中可以可視化的進行操作項目中所有接口。
二、swagger-ui使用
訪問swagger-ui.html后面可以在頁面中看到所有需要生成接口文檔的額控制器名稱。

每個控制器中包含多個所有控制器方法的各種訪問方式。如果使用的是@RequestMapping進行映射,將顯示下面的所有請求方式。如果使用@PostMapping將只有post方式可以能訪問,下面也就只顯示post的一個。

點擊某個請求方式中 try it out

會出現(xiàn)界面要求輸入的值。輸入完成后點擊Execute按鈕

以上就是Spring項目中swagger用法與swagger-ui使用的詳細內(nèi)容,更多關(guān)于swagger用法與swagger-ui使用的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
springBoot不同module之間互相依賴的實現(xiàn)
本文主要介紹了springBoot不同module之間互相依賴的實現(xiàn),不同模塊之間的依賴通常是通過Maven或Gradle來管理的,下面就來介紹一下如何實現(xiàn),感興趣的可以了解一下2024-08-08
Windows下后端如何啟動SpringBoot的Jar項目
這篇文章主要介紹了Windows下后端如何啟動SpringBoot的Jar項目問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):循環(huán)鏈表和棧
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之循環(huán)鏈表、棧的實現(xiàn)方法,結(jié)合實例形式分析了Java數(shù)據(jù)結(jié)構(gòu)中循環(huán)鏈表、棧、的功能、定義及使用方法,需要的朋友可以參考下2021-08-08

