SpringBoot結(jié)合Swagger2自動生成api文檔的方法
首先在pom.xml中添加如下依賴,其它web,lombok等依賴自行添加
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
SwaggerConfig.java是swagger2的配置類
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("cn.niit.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot中使用Swagger2") .description("首次嘗試自動生成api文檔為后期的前后端分離開發(fā)做準(zhǔn)備") .termsOfServiceUrl("https://www.jianshu.com/u/2f60beddf923") .contact("WEN") .version("1.0") .build(); } }
實(shí)體類User.java
@Data @AllArgsConstructor @NoArgsConstructor public class User { public User(String userName, String password) { this.userName = userName; this.password = password; } private Integer id; private String userName; private String password; }
新建一個(gè)控制類UserController.java,類下有個(gè)方法getAllUser
@RestController @Api(description = "用戶管理") @RequestMapping(value = "/hello",produces = APPLICATION_JSON_VALUE) public class UserController { List<User>lists=new ArrayList<>(); @GetMapping(value ="getAllUser" ) @ApiOperation(value = "用戶列表",notes = "查詢所有已注冊過的用戶詳細(xì)信息") public List<User> getAllUser() { lists.add(new User("wen","999")); lists.add(new User(2,"qian","666")); return lists; } }
點(diǎn)擊localhost:8888/swagger-ui.html(我在application.propertise中的server.port=8888)
在類中再添加一個(gè)方法addUser
@PostMapping(value = "addUser") public User addUser(User user) { return user; }
實(shí)體類User.java的屬性上添加如下注解
@ApiModelProperty(value = "用戶ID") private Integer id; @ApiModelProperty(value = "用戶名") private String userName; @ApiModelProperty(value = "密碼") private String password;
創(chuàng)建用戶時(shí)有些字段我們并不需要,可以加入如下注解
@ApiModelProperty(hidden = true)
在類中再添加一個(gè)根據(jù)用戶id查詢用戶的方法
@GetMapping(value = "getUserById/{id}") public User getUserById(@ApiParam(value = "用戶ID")@PathVariable(value = "id")String id) { return new User(id,"步驚云","passwordjava"); }
漢化成中文文檔
在swagger相關(guān)的jar包
把META-INF這個(gè)包復(fù)制到你當(dāng)前項(xiàng)目的resources目錄下
這些是關(guān)鍵,剩下多余的包可自行刪除
在swagger-ui.html的<head>部分添加如下代碼
<!--國際化操作:選擇中文版 --> <script src='webjars/springfox-swagger-ui/lang/translator.js' type='text/javascript'></script> <script src='webjars/springfox-swagger-ui/lang/zh-cn.js' type='text/javascript'></script>
漢化完成,我們也可以在zh_cn.js中自定義中文名稱
另一種生成文檔的方式請參見
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)驗(yàn)證碼的產(chǎn)生和驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)驗(yàn)證碼的產(chǎn)生和驗(yàn)證,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2012-01-01Java SpringBoot整合Canal實(shí)現(xiàn)數(shù)據(jù)同步方式
本文介紹了如何開啟和配置Canal,以及如何在Spring Boot中集成Canal,Canal是一種基于MySQL的數(shù)據(jù)庫變更解析工具,可以將數(shù)據(jù)庫的變更事件發(fā)送到Kafka、RocketMQ等消息隊(duì)列中,用于數(shù)據(jù)分析和挖掘2025-02-02因不會遠(yuǎn)程debug調(diào)試我被項(xiàng)目經(jīng)理嘲笑了
這篇文章主要介紹了遠(yuǎn)程debug調(diào)試的相關(guān)內(nèi)容,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08Java 實(shí)戰(zhàn)圖書管理系統(tǒng)的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實(shí)現(xiàn)一個(gè)圖書管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11Java?Bean轉(zhuǎn)Map的那些踩坑實(shí)戰(zhàn)
項(xiàng)目中有時(shí)會遇到Map轉(zhuǎn)Bean,Bean轉(zhuǎn)Map的情況,下面這篇文章主要給大家介紹了關(guān)于Java?Bean轉(zhuǎn)Map那些踩坑的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07SpringBoot實(shí)現(xiàn)短鏈接系統(tǒng)的使用示例
由于短鏈接可能涉及到用戶隱私和安全問題,所以短鏈接系統(tǒng)也需要符合相關(guān)的數(shù)據(jù)保護(hù)和安全標(biāo)準(zhǔn),本文主要介紹了SpringBoot實(shí)現(xiàn)短鏈接系統(tǒng)的使用示例,感興趣的可以了解一下2023-09-09SpringBoot詳解自定義Stater的應(yīng)用
Springboot的出現(xiàn)極大的簡化了開發(fā)人員的配置,而這之中的一大利器便是springboot的starter,starter是springboot的核心組成部分,springboot官方同時(shí)也為開發(fā)人員封裝了各種各樣方便好用的starter模塊2022-07-07