Swagger在线文档使用教程
1|0springboot整合Swagger2
1、首先创建一个springboot工程,在pom文件内导入依赖
<!--swagger依赖-->
<!--Swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.0</version>
</dependency>
<!--Swagger-ui界面-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.0</version>
</dependency>
2、在springboot工程内的项目运行无误后,创建配置类SwaggerConfig
@Configuration // 配置类
@EnableSwagger2 // 开启Swagger2
public class SwaggerConfig {
/**
* 配置了Swagger的Docket的Bean实例
* 可以有多个
* @return
*/
@Bean
public Docket docket(Environment environment) {
// 获取项目环境
// 设置要显示的Swagger2环境
Profiles profiles = Profiles.of("dev");
// 监听 通过environment.acceptsProfiles判断是否处在自己设定的环境
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
// Select a spec默认值
.groupName("茶碗儿")
// 是否开启swagger2
// .enable(false)
// 监听
.enable(flag)
.select()
// RequestHandlerSelectors 配置要扫描接口的方式
// basePackage 指定扫描的包
// any 扫描全部
// none 不扫描
// withClassAnnotation 扫描类上的注解,参数是一个注解的反射对象
// withMethodAnnotation 扫描方法上的注解
.apis(RequestHandlerSelectors.basePackage("com.changgou.controller"))
// 过滤路径
// .paths(PathSelectors.ant("/changgou/**"))
.build();
}
@Bean
public Docket docket1(Environment environment) {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("花生糖");
}
// 配置Swagger2信息
private ApiInfo apiInfo() {
// 作者信息
Contact contact = new Contact("茶碗儿", "https://www.cnblogs.com/chawaner/", "*******@qq.com");
return new ApiInfo(
// 大标题
"畅购商城",
// 描述
"畅购商城在线API",
// 版本
"v1.0",
// 服务组织url
"https://www.cnblogs.com/chawaner/",
// 作者信息
contact,
// 开源版本号
"Apache 2.0",
//// 开源版本url
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<VendorExtension>());
}
}
3、在JavaBean中加上注解
@ApiModel(description = "Result", value = "Result")
@Data // lombok注解
public class Result<T> implements Serializable {
@ApiModelProperty(value = "执行是否成功,true:成功,false:失败", required = true)
private boolean flag; // 是否成功
@ApiModelProperty(value = "返回状态码,20000:成功,20001:失败,20002:用户名或密码错误,20003:权限不足,20004:远程调用失败,20005:重复操作,20006:没有对应的抢购数据", required = true)
private Integer code; // 返回码
@ApiModelProperty(value = "提示信息", required = true)
private String message; // 返回消息
@ApiModelProperty(value = "逻辑数据", required = true)
private T data; // 返回数据
public Result(boolean flag, Integer code, String message, Object data) {
this.flag = flag;
this.code = code;
this.message = message;
this.data = (T) data;
}
public Result(boolean flag, Integer code, String message) {
this.flag = flag;
this.code = code;
this.message = message;
}
public Result() {
this.flag = true;
this.code = StatusCode.OK;
this.message = "操作成功!";
}
}
4、在controller类中加入注解
@Api(
value = "用户品牌操作",
tags = {"用户品牌操作接口"})
@RestController
@RequestMapping(value = "/brand")
@CrossOrigin // 跨域访问:域名、端口、协议不一致
public class BrandController {
@Autowired private BrandService brandService;
/** 查询所有 */
@ApiOperation("查询所有品牌信息")
@GetMapping
public Result<List<Brand>> findAll() {
// 查询所有品牌信息
List<Brand> brands = brandService.findAll();
// 响应结果封装
return new Result<List<Brand>>(true, StatusCode.OK, "查询品牌集合成功!", brands);
}
/** 根据主键id查询 */
@ApiOperation("据主键id查询") // 接口 放在方法上
@GetMapping(value = "/{id}")
public Result<Brand> findById(
@ApiParam(value = "传入的id") @PathVariable(value = "id") Integer id) {
Brand brand = brandService.findById(id);
return new Result<Brand>(true, StatusCode.OK, "根据id查询品牌成功!", brand);
}
/**
* 增加品牌
*
* @param brand
*/
@ApiOperation("增加品牌")
@PostMapping
public Result add(@ApiParam(value = "要增加的品牌信息") @RequestBody Brand brand) {
brandService.add(brand);
return new Result(true, StatusCode.OK, "添加品牌成功!");
}
/**
* 根据ID修改品牌数据
*
* @param id
* @param brand
* @return
*/
@ApiOperation("根据ID修改品牌信息")
@PutMapping(value = "/{id}")
public Result update(
@ApiParam(value = "输入的id", example = "1") @PathVariable(value = "id") Integer id,
@ApiParam(value = "brand") @RequestBody Brand brand) {
brand.setId(id);
brandService.update(brand);
return new Result(true, StatusCode.OK, "根据ID修改品牌成功!");
}
/**