二、构建Swagger和springboot环境Swagger是一个用来定义接口标准,接口规范,同时能根据你的代码自动生成接口说明文档的一个工具。
构建时碰到一个问题:
Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.NullPointerException
原因是因为springboot的版本太高了,缺少swagger运行所需要的环境。
这里我将springboot的版本降到了2.5.0。
之前springboot版本是2.6.1,而swagger的版本是2.9.2 。
2.1、引入依赖
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
2.2、编写Swagger配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi(){
return new Docket(documentationType.SWAGGER_2)
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.basePackage("com.charry.h.controller"))
.paths(PathSelectors.any())
.build().apiInfo(new ApiInfoBuilder()
.title("Springboot整合Swagger")
.description("Springboot整合Swagger,详细信息...")
.version("2.0")
.contact(new Contact("charryH","www.charry.com","charry@133.com"))
.license("The License")
.licenseUrl("https://www.baidu.com")
.build());
}
}
2.3、开发Controller接口apis:使用什么样的方式来扫描接口。paths:扫描接口的路径。
@RestController
@RequestMapping("user")
public class UserController {
@GetMapping("findAll")
public Map findAll(){
Map map = new HashMap<>();
map.put("success","查询成功!");
map.put("status",true);
return map;
}
}
2.4、访问接口界面
三、Swagger的注解
3.1、@Api
作用:用在指定接口的描述文字修饰范围:用在类上
@RestController
@RequestMapping("user")
@Api(tags = "用户接口")
public class UserController {
}
3.2、@ApiOperation
作用:用来对接口中具体方法做描述修饰范围:用在方法上
@RequestMapping("findAll")
@ApiOperation(value = "查询所有",notes = "详细描述....")
public Map findAll(){
}
3.3、@ApiImplicitParams
作用:用在接口中的参数进行说明修饰范围:用在方法上
@ApiImplicitParams({
@ApiImplicitParam(name = "name",value = "姓名",dataType = "String",defaultValue = "张三"),
@ApiImplicitParam(name = "password",value = "密码",dataType = "String",defaultValue = "157213")
})
public Map save(String name,String password){
}



