引入pom.xml依賴
<!-- swagger2 配置 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.6</version>
</dependency>
配置
在config包下面創(chuàng)建Swagger2Config類
@Configuration
@EnableSwagger2
public class Swagger2 {
// 配置Swagger核心配置 docket
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2) //指定Api類型為Swagger2
.apiInfo(apiInfo()) //用于定義api文檔的匯總信息
.select()
.apis(RequestHandlerSelectors.
basePackage("com.xxxx.controller")) //指定controller包
.paths(PathSelectors.any()) //所有controller
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("XXXXX平臺接口api") //文檔頁標(biāo)題
.contact(new Contact("聯(lián)系人",
"http://rouji4.top",
"552004519@qq.com")) //聯(lián)系人信息
.description("專為XXXXX提供的API文檔") //詳細(xì)信息
.version("1.0.1") //版本信息
.termsOfServiceUrl("http://rouji4.top") //網(wǎng)站信息
.build();
}
}
文檔訪問地址
兩種不同的展示風(fēng)格
注解
- @ApiIgnore 忽略某個類下的接口蹈集,讓其不在文檔中展示
@ApiIgnore
@RestController
public class HelloController {
final static Logger logger= LoggerFactory.getLogger(HelloController.class);
@GetMapping("hello")
public Object Hello(){
logger.info("info:hello!");
return "hello world";
}
}
- @Api 描述對該控制類所包含的功能
@Api(value = "注冊登陸", tags = {"用于注冊登陸的相關(guān)接口"})
@RestController
@RequestMapping("passport")
public class PassportController extends BaseController {}
- @ApiOperation 對某個接口的描述
@ApiOperation(value = "用戶注冊", notes = "用戶注冊", httpMethod = "POST")
@PostMapping("regist")
public IMOOCJSONResult regist(@RequestBody UserBO userBO,
HttpServletRequest request,
HttpServletResponse response) {}
- @ApiModel 可用于對請求實(shí)體的描述
ApiModel(value = "用戶對象BO",description = "從客戶端斗塘,用戶傳入的數(shù)據(jù)封裝在此entity中")
public class UserBO {
@ApiModelProperty(value = "用戶名",name = "username",example = "imooc",required = true)
private String username;
@ApiModelProperty(value = "密碼",name = "password",example = "123456",required = true)
private String password;
@ApiModelProperty(value = "確認(rèn)密碼",name = "confirmPassword",example = "123456",required = false)
private String confirmPassword;
......略
}