SpringBoot介入Swagger 2.0完善 生成接口文档生成,接口测试 SwaggerFox模式的
1. 搭建Swawgger2环境(添加依赖)
io.springfox
springfox-swagger2
2.8.0
io.springfox
springfox-swagger-ui
2.8.0
2. 配置类
@Configuration//创建配置类的注解
@EnableSwagger2//打开支持Swagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")//访问的路径
.select()
.apis(RequestHandlerSelectors.basePackage("com.example"))//扫描注解包所在的位置
.paths(PathSelectors.any())
.build().apiInfo(new ApiInfoBuilder()
.title("智慧景区公共信息云平台后台服务接口列表")//接口大标题
.description("服务中心,运营中心等相关接口说明...")//接口说明
.version("2.1")//版本号
.contact(new Contact("大老板","blog.csdn.net","aaa@gmail.com"))
.license("China dci")
.licenseUrl("http://www.chinadci.com")
.build());
}
}
3. 接口中的
@RestController
@Api(tags = "学生的接口")//接口类的说明
public class StudentController {
@ApiOperation(value = "查询学生的信息")//接口说明
@GetMapping("SelectStudent")
public Object SelectStudent(){
return "查询学生..............";
}
@PostMapping("InsertStudent")
@ApiOperation(value = "添加学生")//接口说明
@ApiImplicitParams(//接口参数说明
{
@ApiImplicitParam(name = "name", value = "学生名字"),
@ApiImplicitParam(name = "pwd", value = "学生密码")
}
)
public Object InsertStudent(String name,String pwd){
return "添加学生..............";
}
}
4. 实体类
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "学生的Model")//实体类的说明
public class Student {
@ApiModelProperty(value = "学生id")//字段说明
private Integer id;
@ApiModelProperty(value = "学生姓名")//字段说明
private String name;
@ApiModelProperty(value = "学生密码")//字段说明
private String pwd;
}
5. 启动后 访问 http://localhost:8080/swagger-ui.html