目录
1、引入swagger依赖
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
2、编辑配置文件
package com.jt.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @Author Connor
* @Date: 2023/07/31/ 14:47
* @Description
*/
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean(value = "cat")
public Docket defaultApi2() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(new ApiInfoBuilder()
.title("这是车辆模块API ")
.description("# 车辆模块所有的接口的入参,出参等等信息")
.termsOfServiceUrl("http://cat.com")
.contact(new Contact("connor","http://cat.com","1275xxx511@qq.com"))
.version("1.0")
.build())
//分组名称
.groupName("车辆模块")
.select()
//这里指定Controller扫描包路径
.apis(RequestHandlerSelectors.basePackage("com.jt.controller.cat"))
.paths(PathSelectors.any())
.build();
}
@Bean(value = "item")
public Docket defaultApi3() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(new ApiInfoBuilder()
.title("这是其他API ")
.description("# 车辆模块所有的接口的入参,出参等等信息")
.termsOfServiceUrl("http://cat.com")
.contact(new Contact("connor","http://cat.com","12756xxx8511@qq.com"))
.version("1.0")
.build())
//分组名称
.groupName("item模块")
.select()
//这里指定Controller扫描包路径
.apis(RequestHandlerSelectors.basePackage("com.jt.controller.item"))
.paths(PathSelectors.any())
.build();
}
}
3、对应controller层加上swagger注解
package com.jt.controller.cat;
import com.jt.pojo.ItemCat;
import com.jt.service.ItemCatService;
import com.jt.vo.SysResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/itemCat")
@Api(value ="车辆模块",tags = "车辆模块")
public class ItemCatController {
@Autowired
private ItemCatService itemCatService;
/**
* 请求路径: /itemCat/findItemCatList/{level}
* 请求类型: get
* 请求参数: level
* 返回值: SysResult(List集合)
*/
@GetMapping("/findItemCatList/{level}")
@ApiOperation(value = "查询商品分类列表",notes = "根据level查询商品分类列表")
public SysResult findItemCatList(@PathVariable Integer level){
List<ItemCat> list = itemCatService.findItemCatList(level);
return SysResult.success(list);
}
/**
* - 请求路径: /itemCat/status
* - 请求类型: put
* - 返回值: SysResult对象
*/
@PutMapping("/status/{id}/{status}")
@ApiOperation("更新车辆")
public SysResult updateStatus(ItemCat itemCat){
itemCatService.updateStatus(itemCat);
return SysResult.success();
}
/**
* 实现商品分类新增
* - 请求路径: /itemCat/saveItemCat
* - 请求类型: post
* - 请求参数: 表单数据
* - 返回值结果: SysResult对象
*/
@PostMapping("/saveItemCat")
public SysResult saveItemCat(@RequestBody ItemCat itemCat){
itemCatService.saveItemCat(itemCat);
return SysResult.success();
}
/**
* - 请求路径: /itemCat/updateItemCat
* - 请求类型: put
* - 请求参数: 表单数据 ItemCat对象
* - 返回值: SysResult对象
*/
@PutMapping("/updateItemCat")
public SysResult updateItemCat(@RequestBody ItemCat itemCat){
itemCatService.updateItemCat(itemCat);
return SysResult.success();
}
/**
* - 请求路径: /itemCat/deleteItemCat?id=xx&level=xx
* - 请求类型: delete
* - 业务描述: 当删除节点为父级时,应该删除自身和所有的子节点
* - 请求参数: id/level
* - 返回值: SysResult对象
*/
@DeleteMapping("/deleteItemCat")
public SysResult deleteItemCat(ItemCat itemCat){
itemCatService.deleteItemCat(itemCat);
return SysResult.success();
}
}
4、定义返回的vo类加上swagger注解-作用为字段注释
package com.jt.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.List;
/**
* @author connor
* 时间 2021/3/26
*/
@Data
@Accessors(chain = true)
@TableName("item_cat")
public class ItemCat extends BasePojo{
@TableId(type = IdType.AUTO)//主键自增
@ApiModelProperty("定义主键")
private Integer id;
@ApiModelProperty("定义父级菜单")
private Integer parentId;
@ApiModelProperty("定义父级菜单")
private String name;
@ApiModelProperty("分类名称")
private Boolean status;
@ApiModelProperty("商品分类等级 1 2 3")//分类状态 0 停用 1 正常
private Integer level; //商品分类等级 1 2 3
@TableField(exist = false)
private List<ItemCat> children; //是业务数据 不是数据库字段
}
5、启动项目访问swagger地址
启动项目访问http://localhost:8093/doc.html,注意改成自己项目的端口号
到此swagger引入完成,可以愉快的交给前端联调接口了。