SpringBoot結(jié)合Swagger2自動(dòng)生成api文檔的方法
首先在pom.xml中添加如下依賴(lài),其它web,lombok等依賴(lài)自行添加
<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的配置類(lèi)
@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("首次嘗試自動(dòng)生成api文檔為后期的前后端分離開(kāi)發(fā)做準(zhǔn)備") .termsOfServiceUrl("https://www.jianshu.com/u/2f60beddf923") .contact("WEN") .version("1.0") .build(); } }
實(shí)體類(lèi)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è)控制類(lèi)UserController.java,類(lèi)下有個(gè)方法getAllUser
@RestController @Api(description = "用戶(hù)管理") @RequestMapping(value = "/hello",produces = APPLICATION_JSON_VALUE) public class UserController { List<User>lists=new ArrayList<>(); @GetMapping(value ="getAllUser" ) @ApiOperation(value = "用戶(hù)列表",notes = "查詢(xún)所有已注冊(cè)過(guò)的用戶(hù)詳細(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)
在類(lèi)中再添加一個(gè)方法addUser
@PostMapping(value = "addUser") public User addUser(User user) { return user; }
實(shí)體類(lèi)User.java的屬性上添加如下注解
@ApiModelProperty(value = "用戶(hù)ID") private Integer id; @ApiModelProperty(value = "用戶(hù)名") private String userName; @ApiModelProperty(value = "密碼") private String password;
創(chuàng)建用戶(hù)時(shí)有些字段我們并不需要,可以加入如下注解
@ApiModelProperty(hidden = true)
在類(lèi)中再添加一個(gè)根據(jù)用戶(hù)id查詢(xún)用戶(hù)的方法
@GetMapping(value = "getUserById/{id}") public User getUserById(@ApiParam(value = "用戶(hù)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>部分添加如下代碼
<!--國(guó)際化操作:選擇中文版 --> <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中自定義中文名稱(chēng)
另一種生成文檔的方式請(qǐng)參見(jiàn)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)簡(jiǎn)易學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)易學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07Mybatis動(dòng)態(tài)Sql標(biāo)簽使用小結(jié)
本文主要介紹了Mybatis動(dòng)態(tài)Sql標(biāo)簽使用,常用的動(dòng)態(tài)sql標(biāo)簽包括?if、choose(when、otherwise)、trim(where、set)、foreach,下面就來(lái)介紹一下2024-04-04簡(jiǎn)單理解java泛型的本質(zhì)(非類(lèi)型擦除)
泛型在java中有很重要的地位,在面向?qū)ο缶幊碳案鞣N設(shè)計(jì)模式中有非常廣泛的應(yīng)用。泛型是參數(shù)化類(lèi)型的應(yīng)用,操作的數(shù)據(jù)類(lèi)型不限定于特定類(lèi)型,可以根據(jù)實(shí)際需要設(shè)置不同的數(shù)據(jù)類(lèi)型,以實(shí)現(xiàn)代碼復(fù)用。下面小編來(lái)簡(jiǎn)單講一講泛型2019-05-05Java設(shè)計(jì)模式中代理模式應(yīng)用詳解
代理模式(Proxy Parttern)為一個(gè)對(duì)象提供一個(gè)替身,來(lái)控制這個(gè)對(duì)象的訪問(wèn),即通過(guò)代理對(duì)象來(lái)訪問(wèn)目標(biāo)對(duì)象。本文將通過(guò)示例詳細(xì)講解一下這個(gè)模式,需要的可以參考一下2022-11-11SpringBoot+Mybatis使用Mapper接口注冊(cè)的幾種方式
本篇博文中主要介紹是Mapper接口與對(duì)應(yīng)的xml文件如何關(guān)聯(lián)的幾種姿勢(shì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07