Spring項(xiàng)目中swagger用法與swagger-ui使用
一、swagger用法
1.1、編寫(xiě)springboot項(xiàng)目
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依賴
在項(xiàng)目pom.xml中導(dǎo)入spring-fox依賴,該項(xiàng)目選擇版本為2.9.2。其中springfox-swagger2是核心內(nèi)容的封裝。springfox-swagger-ui是對(duì)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的啟動(dòng)類中添加 @EnableSwagger2 注解。
添加此注解后表示對(duì)當(dāng)前項(xiàng)目中全部控制器進(jìn)行掃描。應(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提供的一個(gè)注解,代表swagger2相關(guān)技術(shù)開(kāi)啟。 * 會(huì)掃描當(dāng)前類所在包,及子包中所有的類型中的注解。 */ @SpringBootApplication @EnableSwagger2 public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class,args); } }
1.4、訪問(wèn)swagger-ui
啟動(dòng)項(xiàng)目后在瀏覽器中輸入 http://ip:port/swagger-ui.html 既可以訪問(wèn)到swagger-ui頁(yè)面,在頁(yè)面中可以可視化的進(jìn)行操作項(xiàng)目中所有接口。
二、swagger-ui使用
訪問(wèn)swagger-ui.html后面可以在頁(yè)面中看到所有需要生成接口文檔的額控制器名稱。
每個(gè)控制器中包含多個(gè)所有控制器方法的各種訪問(wèn)方式。如果使用的是@RequestMapping進(jìn)行映射,將顯示下面的所有請(qǐng)求方式。如果使用@PostMapping將只有post方式可以能訪問(wèn),下面也就只顯示post的一個(gè)。
點(diǎn)擊某個(gè)請(qǐng)求方式中 try it out
會(huì)出現(xiàn)界面要求輸入的值。輸入完成后點(diǎn)擊Execute按鈕
以上就是Spring項(xiàng)目中swagger用法與swagger-ui使用的詳細(xì)內(nèi)容,更多關(guān)于swagger用法與swagger-ui使用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
springboot對(duì)接微信支付的完整流程(附前后端代碼)
最近在做支付平臺(tái)的項(xiàng)目,承接公司業(yè)務(wù)系統(tǒng)與第三方支付平臺(tái)的對(duì)接任務(wù),主要涉及微信支付、支付寶支付以及理房通支付等第三方平臺(tái),這篇文章主要給大家介紹了關(guān)于springboot對(duì)接微信支付的完整流程,需要的朋友可以參考下2021-08-08springBoot不同module之間互相依賴的實(shí)現(xiàn)
本文主要介紹了springBoot不同module之間互相依賴的實(shí)現(xiàn),不同模塊之間的依賴通常是通過(guò)Maven或Gradle來(lái)管理的,下面就來(lái)介紹一下如何實(shí)現(xiàn),感興趣的可以了解一下2024-08-08Windows下后端如何啟動(dòng)SpringBoot的Jar項(xiàng)目
這篇文章主要介紹了Windows下后端如何啟動(dòng)SpringBoot的Jar項(xiàng)目問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07基于Java實(shí)現(xiàn)簡(jiǎn)單的郵件群發(fā)功能
這篇文章主要為大家詳細(xì)介紹了如何利用Java語(yǔ)言編寫(xiě)一個(gè)簡(jiǎn)單的工具類,可以實(shí)現(xiàn)郵件群發(fā)功能。文中的示例代碼講解詳細(xì),需要的可以參考一下2022-05-05Spring計(jì)時(shí)器stopwatch使用詳解
這篇文章主要介紹了Spring計(jì)時(shí)器stopwatch使用詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Spring為何需要三級(jí)緩存解決循環(huán)依賴詳解
這篇文章主要給大家介紹了關(guān)于Spring為何需要三級(jí)緩存解決循環(huán)依賴,而不是二級(jí)緩存的相關(guān)資料,這個(gè)也是一個(gè)Spring的高頻面試題,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):循環(huán)鏈表和棧
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之循環(huán)鏈表、棧的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Java數(shù)據(jù)結(jié)構(gòu)中循環(huán)鏈表、棧、的功能、定義及使用方法,需要的朋友可以參考下2021-08-08