一,添加依賴
Swagger需要依賴兩個jar包,在pom.xml中添加如下坐標
<dependency>
<groupId>io.springfox<groupId>
<artifactId>springfox-swagger2<artifactId>
<version>2.2.2<version>
<dependency>
<dependency>
<groupId>io.springfox<groupId>
<artifactId>springfox-swagger-ui<artifactId>
<version>2.2.2<version>
<dependency>
二培己,創(chuàng)建配置類
Swagger需要一個配置類來進行對swagger的基本配置
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean public Docket createRestApi()
{
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select() .apis(RequestHandlerSelectors.basePackage("com.netinfo.controller")) .paths(PathSelectors.any()) .build();
}
private ApiInfo apiInfo()
{
return
new ApiInfoBuilder()
.title("boot_demo")
.description("如需幫助請xxxxxx電話")
.contact("zy")
.version("1.0") .build();
}
}
第三步:Controller配置
@Api("UCC自定義接口")
@RestController
public class HelloWorldController {
private static Logger logger =LoggerFactory.getLogger(HelloWorldController.class); @RequestMapping("/say.html")
@ApiOperation(value = "測試接口",notes = "集成swagger的接口",httpMethod = "POST",produces = "application/json")
public String say(){ String method="say"; logger.info("調(diào)用接口:{}",method); return "Hello Spring Boot"; } }
啟動application 并訪問http://localhost:8088/swagger-ui.html
大功告成碳蛋!