首先导入swagger2依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
Swagger2配置
package com.vue.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi(){
Docket docket = new Docket(DocumentationType.SWAGGER_2).pathMapping("/").select()
.apis(RequestHandlerSelectors.basePackage("com.vue.controller"))
.build().apiInfo(new ApiInfoBuilder().title("vue5web").description("项目详细信息")
.version("1.0").contact(new Contact("aaa", "blog.csdn.net", "aaa@qq.com")
).license("this apache license").licenseUrl("http://www.baidu.com").build());
return docket;
}
}
通过@EnableSwagger2注解启用Swagger2,然后配置一个DocketBean。在这个bean中,配置映射路径和接口扫描位置。在apiInfo中主要配置Swagger2的文档网站信息。
此时启动项目,输入http://localhost:8023/swagger-ui.html 能看到如下界面,说明Swagger已经配置成功了。
@RestController
/*@CrossOrigin*/
@Api(tags = "用户管理相关接口")
public class StudentController {
@Autowired
private StudentMapper studentMapper;
@ApiOperation("查询全部用户")
@GetMapping("/findAllStudent")
public List<Student> findAllStudent(){
return studentMapper.findAllStudent();
}
@GetMapping("/findAllPageInfo")
@ApiOperation("分页查询全部用户")
/* @ApiImplicitParams({
@ApiImplicitParam(name="pageNumber",value = "当前页"),
@ApiImplicitParam(name = "pageSize",value = "每页条数")
})*/
public PageInfo<Student> findAllPageInfo(@RequestParam(name = "pageNumber",required=false,defaultValue = SystemConstant.PAGE_NUM) Integer pageNumber,
@RequestParam(name = "pageSize",required = false,defaultValue = SystemConstant.PAGE_SIZE)Integer pageSize){
PageHelper.startPage(pageNumber,pageSize);
return new PageInfo<>(studentMapper.findAllStudent());
}
@ApiOperation("添加用户信息")
@PostMapping("/addStudent")
public void addStudent(Student student){
studentMapper.addStudent(student);
}
}
类
@Api(tags = “”)标记当前controller的功能
方法
@ApiOperation("")表示当前方法的功能
参数
@ApiImplicitParam()只有一个参数时用这个注解
参数多的情况
@ApiImplicitParams({
@ApiImplicitParam(name=“pageNumber”,value = “当前页”),
@ApiImplicitParam(name = “pageSize”,value = “每页条数”)
})
@ApiImplicitParam(name=“pageNumber”,value = “当前页”)该注解中的一些方法并不能代替@RequestParam注解
参数时一个对象
@ApiModel
@Data
public class Student implements Serializable {
@ApiModelProperty(value = "主键")
private Integer stuid;
@ApiModelProperty(value = "姓名")
private String stuName;
@ApiModelProperty(value = "性别")
private String stuGender;
@ApiModelProperty(value = "具体地址")
private String stuAddr;
@ApiModelProperty("年龄")
private Integer stuAge;
}
都配置完成后可以看到如下界面