Spring Boot集成swagger2

一,添加依赖

Swagger需要依赖两个jar包,在pom.xml中添加如下坐标

<dependency>

<groupId>io.springfox

<artifactId>springfox-swagger2

<version>2.2.2

<dependency>

<groupId>io.springfox

<artifactId>springfox-swagger-ui

<version>2.2.2

二,创建配置类

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("调用接口:{}",method); return "Hello Spring Boot"; } }

启动application 并访问http://localhost:8088/swagger-ui.html

大功告成!

你可能感兴趣的:(Spring Boot集成swagger2)