1 創(chuàng)建SpringBoot Demo項目
- 創(chuàng)建Maven項目
- 修改pom文件,改為springboot項目,增加springfox依賴。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
注意:spring-boot-starter-parent 2.6版本以上在啟動時會報錯
2 項目代碼
package com.doc.controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.doc.BaseRespVO;
import com.doc.TestReqVO;
import com.doc.TestRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
/**
* 測試類
*
* @author young
* @since 2021-04-29
*
*/
@RestController
@RequestMapping("/test")
@Api(tags = "測試Controller相關(guān)接口")
public class TestController {
/**
* 測試方法1
*
* @param test
* @return
*/
@RequestMapping(value = "req-test", method = RequestMethod.GET)
@ApiOperation(value = "測試方法1", notes = "測試方法1的相關(guān)備注事項", httpMethod = "GET", responseContainer = "測試方法1Response")
public BaseRespVO<TestRespVO> reqTest(@ApiParam(name = "test", value = "測試字符串", required = true) String test) {
BaseRespVO<TestRespVO> respVO = new BaseRespVO<>();
return respVO;
}
/**
* 測試方法2
*
* @param reqVO
* @return
*/
@RequestMapping(value = "resp-test", method = RequestMethod.POST)
@ApiOperation(value = "測試方法2", notes = "測試方法2的相關(guān)備注事項", httpMethod = "POST", responseContainer = "測試方法2Response")
public BaseRespVO<Void> respTest(
@RequestBody @ApiParam(name = "reqVO", value = "測試請求對象", required = true) TestReqVO reqVO) {
BaseRespVO<Void> respVO = new BaseRespVO<>();
return respVO;
}
}
package com.doc;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(value = "測試請求類")
public class TestReqVO {
/**
* 國家
*/
@ApiModelProperty(value = "國家")
private String country;
/**
* 省
*/
@ApiModelProperty(value = "省")
private String province;
/**
* 市
*/
@ApiModelProperty(value = "市")
private String city;
/**
* 姓名
*/
@ApiModelProperty(value = "姓名")
private String name;
/**
* 年齡
*
*/
@ApiModelProperty(value = "年齡")
private int age;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.doc;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(value = "測試響應(yīng)類")
public class TestRespVO {
/**
* 唯一id
*/
@ApiModelProperty(value = "唯一id")
private int id;
/**
* 隨機碼uuid
*
*/
@ApiModelProperty(value = "隨機碼uuid")
private String uuid;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
}
package com.doc;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* 返回數(shù)據(jù)Base對象
*/
@ApiModel(value = "返回數(shù)據(jù)Base對象")
public class BaseRespVO<T> {
/**
* 返回碼
*
*/
@ApiModelProperty(value = "返回碼")
private int code = 0;
/**
* 返回碼描述信息
*
*/
@ApiModelProperty(value = "返回碼描述信息")
private String desc;
/**
* 返回數(shù)據(jù)
*
*/
@ApiModelProperty(value = "返回數(shù)據(jù)")
protected T data;
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return super.toString();
}
}
3 配置swagger
配置config:
package com.doc.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
@EnableOpenApi
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo()).enable(true).groupName("Test").select()
.apis(RequestHandlerSelectors.basePackage("com.doc.controller")).build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("Api Doc").description("測試接口").version("1.0").build();
}
}
啟動代碼,輸入地址http://localhost:8080/swagger-ui/
4 結(jié)果預(yù)覽

Swagger.png