knife4j可以通过访问本地页面,实现对项目接口的管理:
这样我们就不需要用postman软件调用接口了。
Mevan导包:
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> </dependency>
配置 knife4j:
package com.quxiao.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
import java.util.ArrayList;
import java.util.List;
/**
* @program: yghtgl
* @author: quxiao
* @create: 2023-06-12 08:44
**/
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {
@Bean
public Docket adminApiConfig(){
List<Parameter> pars = new ArrayList<>();
ParameterBuilder tokenPar = new ParameterBuilder();
tokenPar.name("token")
.description("用户token")
.defaultValue("")
.modelRef(new ModelRef("string"))
.parameterType("header")
.required(false)
.build();
pars.add(tokenPar.build());
//添加head参数end
Docket adminApi = new Docket(DocumentationType.SWAGGER_2)
.groupName("adminApi")
.apiInfo(adminApiInfo())
.select()
//只显示admin路径下的页面
.apis(RequestHandlerSelectors.basePackage("com.quxiao"))
.paths(PathSelectors.regex("/admin/.*"))
.build()
.globalOperationParameters(pars);
return adminApi;
}
private ApiInfo adminApiInfo(){
return new ApiInfoBuilder()
.title("后台管理系统-API文档")
.description("本文档描述了后台管理系统微服务接口定义")
.version("1.0")
.contact(new Contact("quxiao", "http://quxiao.com", "1458798260@qq.com"))
.build();
}
}
然后再后端接口上加上注解:
package com.quxiao.controller;
import com.quxiao.pojo.SysRole;
import com.quxiao.service.SysRoleServiceImpl;
import com.quxiao.util.Result;
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;
/**
* @program: yghtgl
* @author: quxiao
* @create: 2023-06-11 09:45
**/
@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
@Autowired
SysRoleServiceImpl service;
//查询all记录
@ApiOperation(value = "查询all记录")
@GetMapping("sysRoleSelAll")
public Result<List<SysRole>> sysRoleSelAll() {
return Result.ok(service.list());
}
//删除sysRoleDel
@ApiOperation(value = "删除sysRoleDel")
@DeleteMapping("remove/{id}")
public Result<Boolean> sysRoleDel(@PathVariable Long id) {
boolean b = service.removeById(id);
if (b) {
return Result.ok(b);
}
return Result.fail(b);
}
}
再访:http://localhost:8080/doc.html
就可以了