1.添加依赖
<!-- swagger用于定义API文档 -->
<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>
<!--美化swagger-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.3</version>
</dependency>
2.配置类SwaggerConfig
新建config包和SwaggerConfig类
package com.lvxk.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
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;
/**
* SwaggerConfig
* Description: <br/>
* date: 2020/5/5 8:02<br/>
*
* @author lvxk<br />
* @since JDK 1.8
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.lvxk.demo.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("小凯 - demo测试接口")
.description("小凯 - demo测试接口")
.termsOfServiceUrl("http://localhost:8081/swagger-ui.html")
.contact(new Contact("xiaokai.lv", "http://localhost:8071/swagger-ui.html", "lvxiaokai@aliyun.com"))
.version("1.0")
.build();
}
}
3.添加测试类DemoController
创建controller包和DemoController测试类
package com.lvxk.demo.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* DemoController
* Description: <br/>
* date: 2020/5/5 8:05<br/>
*
* @author lvxk<br />
* @since JDK 1.8
*/
@Api(tags = "HELLO模块测试")
@RestController
public class DemoController {
@GetMapping("hello")
@ApiOperation(value = "测试hello的接口")
public String helloTest(){
return "hello,success";
}
}
网友评论