swagger技术
一 swagger技术的作用:
可以用于测试请求,快速生成接口文档。
1.导包(2个依赖包)
<!-- swagger2 -->
<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>
2.配置
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket buildDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(buildApiInfo())
.select()
// 要扫描的API(Controller)基础包
.apis(RequestHandlerSelectors.basePackage("com.ding.swagger"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo buildApiInfo() {
Contact contact = new Contact("xxxxx","","");
return new ApiInfoBuilder()
.title("测试平台-平台管理API文档")
.description("平台管理服务api")
.contact(contact)
.version("1.0.0").build();
}
}
3.启动类
@SpringBootApplication
public class SwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerApplication.class, args);
}
}
4.业务(加注解)
Controller层的接口上:
@Api(value = "测试频道管理", tags = "channel", description = "测试频道管理API")
public interface ChannelControllerApi {
@ApiOperation("ID查询")
AdChannel findById(Long id);
}
实体类的属性上:
@Data
public class ChannelDto extends PageRequestDto {
/**
* 频道名称
*/
@ApiModelProperty("频道名称")
private String name;
}
5.测试:http:// IP : PORT /swagger-ui.html