第一步:添加swagger依赖
你可以在idea里面按ctrl+insert快速查找添加依赖
第二步: 编写swagger配置类
/**
* @description:swagger配置类
* @author: Mr.Li
* @create: 2019-01-15 10:40
**/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket crateSwaggerApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//需要产生API的包
.apis(RequestHandlerSelectors.basePackage("com.springboot.shop.controller"))
.paths(PathSelectors.any())
.build();
}
/**
* 构建api文档的详细信息函数,注意这里的注解引用的是哪个。
* @return
*/
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.description("API描述")
//页面标题
.title("spring boot测试使用Swagger2生成restful API")
//版本号
.version("1.0.0")
//创建人
.contact(new Contact("Likai","http://www.baidu.com",""))
.build();
}
第三步:编写测试类测试
@RestController
public class SwaggerController {
@ApiOperation(value = "返回Hello+姓名",notes = "得到姓名")
@ApiImplicitParam(name = "name",required = true,value = "姓名",dataType = "String",paramType = "path")
@RequestMapping("/swagger/{name}")
public String hello(@PathVariable String name){
return "Hello:"+name;
}
}
注意:当name为pathvariable时,paramtype必须设置为path
第四步:启动项目,访问http://localhost:8080/swagger-ui.html