一、创建配置类(标红处为扫描路径)
package com.zjy.springboot.myconfig;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.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;@Configuration@EnableSwagger2WebMvcpublic class Knife4jConfiguration {@Bean(value = "dockerBean")public Docket dockerBean() {//指定使用Swagger2规范Docket docket = new Docket(DocumentationType.SWAGGER_2).apiInfo(new ApiInfoBuilder()//描述字段支持Markdown语法.description("# Knife4j RESTful APIs").termsOfServiceUrl("https://doc.xiaominfo.com/").contact("jiyou.zhang@aliyun.com").version("1.0").build())//分组名称.groupName("你好").select()//这里指定Controller扫描包路径.apis(RequestHandlerSelectors.basePackage(" com.zjy.springboot.controller")).paths(PathSelectors.any()).build();return docket;}}
二、pom.xml 加入依赖
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-openapi2-spring-boot-starter</artifactId><version>4.0.0</version></dependency>
三、关于实体类的标注
package com.zjy.springboot.vo;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Accessors
public class Student {
@ApiModelProperty("sid")
private String sid;
@ApiModelProperty("学生姓名")
private String stuName;
@ApiModelProperty("学生年龄")
private Integer stuAge;
@ApiModelProperty("学生地址")
private String stuAddress;
}
四、controller(接口)
package com.zjy.springboot.controller;
import com.zjy.springboot.vo.Student;
import io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@Api(tags ="学生管理")
@RestController
public class KnifeController {
@ApiOperation(value = "新增学生") //该方法的说明
@RequestMapping(value = "/stu",method = RequestMethod.POST)
public String insert (@RequestBody Student student) { //@validated 检测数据是否正确
System.out.println(student.toString());
return student.toString();
}
@ApiOperation(value = "删除学生")
@RequestMapping(value = "/stu/{sid}", method = RequestMethod.DELETE)
public boolean del(@PathVariable("sid") String sid) {
return true;
}
@ApiOperation(value = "查询学生") //方法的说明
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query",name = "sid",example = "101",dataTypeClass =Student.class ),
@ApiImplicitParam(paramType = "query",name = "sName",example = "张三",dataTypeClass =Student.class ),
@ApiImplicitParam(paramType = "query",name = "sAge",example = "22",dataTypeClass =Student.class ),
@ApiImplicitParam(paramType = "query",name = "sAddress",example = "山西太原",dataTypeClass =Student.class ),
}) // 参数的说明
@ApiResponses({
@ApiResponse(code = 200,message = "成功"),
@ApiResponse(code = 403,message = "权限不足"),
@ApiResponse(code = 404,message = "资源不存在"),
@ApiResponse(code = 500,message = "方法异常"),
})
@RequestMapping(value = "/student",method = RequestMethod.GET)
public List<Student> students(@RequestParam(value = "sid",required = true)String sid,
@RequestParam(value = "sName",required = false) String name,
@RequestParam("sAge") Integer age,
@RequestParam("sAddress")String address) {
List<Student> students = new ArrayList<>();
students.add(new Student("1", "张三", 23, "山西太原"));
students.add(new Student("2", "李四", 23, "山西太原"));
return students;
}
@ApiOperation(value = "修改学生")
@RequestMapping(value = "/stu",method = RequestMethod.PUT)
public int update(@RequestBody Student student) {
return 1;
}
}
访问地址:localhost:8080/doc.html