导入依赖
<!-- swagger -->
<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>
设置swagger
@Configuration
@EnableSwagger2
public class SwaggerConfig {
/**
* 配置Swagger的Docket的bean实例
*/
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("组名")
.select()
// 配置扫描接口的方式
.apis(RequestHandlerSelectors.basePackage("com.lin"))
.build();
}
/**
* 配置Info信息
* @return
*/
private ApiInfo apiInfo(){
// 作者信息
Contact contact = new Contact("", "", "");
return new ApiInfo("我是标题",
"描述",
"1.0", "urn:tos",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
- 设置扫描接口方式RequestHandlerSelectors.
- basePackage(“com.lin”)配置要扫描的接口
- any():扫描包
- none():不扫描
常用注解
- 实体类
@ApiModel("实体类名")
@ApiModelProperty("属性名")
@Data
@ApiModel("学生")
public class Student {
@ApiModelProperty("学号")
private Long studentId;
@ApiModelProperty("学生姓名")
private String studentName;
private Integer studentAge;
private List<Course> courseList;
}
- 接口注释
@ApiOperation("接口说明")
@ApiParam("id号")
@ApiOperation("接口说明")
@GetMapping("/find")
@ResponseBody
public Student findStudentById(@ApiParam("id号")Long id){
Student student = studentService.selectByPrimaryKey(id);
return student;
}
- 类
-
@Api(value = "/template", tags = {"接口名称"})
-
方法返回对象的说明
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 400, message = "Bad Request"), @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 403, message = "Forbidden"), @ApiResponse(code = 404, message = "Not Found"), @ApiResponse(code = 500, message = "Internal Server Error") })
-