引入pom
<!--springboot使用的是2.1.4.RELEASE版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<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>
配置swagger配置類(lèi)
SwaggerConfig.class
package com.lee.room.config;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.function.Predicate;
import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.PathSelectors.regex;
/**
* @Author li.heng
* @Date 2019/6/25 17
* @Description: EnableSwagger2注解 開(kāi)啟Swagger支持
**/
@EnableSwagger2
@Configuration
@ComponentScan("com.lee.room.controller.*")
public class SwaggerConfig {
@Bean
public Docket swaggerCoreConfig() {
// 構(gòu)造函數(shù)傳入初始化規(guī)范琢歇,這是swagger2規(guī)范
return new Docket(DocumentationType.SWAGGER_2)
// 添加api詳情信息
.apiInfo(getInfo())
// 添加默認(rèn)參數(shù)列表
.globalOperationParameters(new ArrayList<>())
.select()
// 添加過(guò)濾條件,
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
// 這里是控制哪些路徑的api會(huì)被顯示出來(lái)兰怠,比如下方的參數(shù)就是除了/user以外的其它路徑都會(huì)生成api文檔
.paths((String a) ->
!a.equals("/user"))
.build();
}
private ApiInfo getInfo() {
// contact為聯(lián)系信息對(duì)象,在訪問(wèn)接口頁(yè)面時(shí)會(huì)呈現(xiàn)李茫,參數(shù)一為名稱(chēng)揭保,參數(shù)二為聯(lián)系url,參數(shù)三為
return new ApiInfoBuilder()
.contact(
new Contact("小籠包",
"https://github.com/lhdhr5828",
"873093067@qq.com"))
.title("room api")
.description("房產(chǎn)信息project")
.termsOfServiceUrl("https://www.baidu.com")
.extensions(new ArrayList<>(16))
.license("許可信息")
.version("1.0").build();
}
}
新建Controller
IndexController.class
package com.lee.room.controller;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author heng.li
* @Date 2019/4/11 13
* @Description
**/
@RestController
public class IndexController {
/**
* @return
*/
@ApiOperation(value = "index接口")
@GetMapping(value = "/")
public String index() {
return "This is room project";
}
/**
* tags 類(lèi)似于group魄宏,下面會(huì)有一系列的接口
* httpMethod是指接口頁(yè)面上顯示的請(qǐng)求方法
* @param str
* @return
*/
@ApiOperation(value = "測(cè)試有參數(shù)接口", notes = "note", tags = "tags", httpMethod = "POST", protocols = "protocols")
@PostMapping(value = "/get/str")
public String getInfo(String str) {
return "test param";
}
}
application.properties
server.port=8080
啟動(dòng)項(xiàng)目
訪問(wèn)http://localhost:8080/swagger-ui.html
點(diǎn)擊接口還可以進(jìn)行請(qǐng)求測(cè)試
測(cè)試結(jié)果: