文章目录
- 添加依赖
- config工作包中新增SwaggerConfig
- 报错
- 注解
环境:
jdk1.8
java8
springboot2.6.13
swagger2.9.2
添加依赖
pom.xml
<!-- 添加swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- 添加swagger-ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
config工作包中新增SwaggerConfig
SwaggerConfig
package com.example.demo.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.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2 //启用swagger2功能
public class SwaggerConfig {
/**
* 配置swagger2相关的bean
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com"))
.paths(PathSelectors.any())
.build();
}
/**
* 此处主要是API文档页面显示信息
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SpringBoot整合Swagger2")
.description("SpringBoot整合Swagger2")
.termsOfServiceUrl("")
.version("1.0")
.build();
}
}
报错
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
解决:
//application.properties
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
注解
作用范围 | API | API常用参数 | 作用位置 |
---|---|---|---|
协议集描述 | @Api | @Api(tags = {“tag1”,“tag2”,“…”}) | controller类 |
协议描述 | @ApiOperation | @ApiOperation(value = “功能描述”,notes = “备注”) | controller类的方法 |
描述返回对象的意义 | @ApiModel | @ApiModel(value=“类名”,description=“类描述”) | 返回对象类 |
对象属性 | @ApiModelProperty | @ApiModelProperty(value = “类属性描述”,required = true,example = “属性举例”,notes = “备注”) | 出入参数对象的字段 |
非对象参数集 | @ApiImplicitParams | @ApiImplicitParams({@ApiImplicitParam(),@ApiImplicitParam(),…}) | controller的方法 |
非对象参数描述 | @ApiImplicitParam | @ApiImplicitParam(name = “参数名”,value = “参数描述”,required = true,paramType = “接口传参类型”,dataType = “参数数据类型”) | @ApiImplicitParams的方法里用 |
Response集 | @ApiResponses | @ApiResponses({ @ApiResponse(),@ApiResponse(),…}) | controller的方法 |
Response | @ApiResponse | @ApiResponse(code = 10001, message = “返回信息”) | @ApiResponses里用 |
忽略注解 | @ApiIgnore | @ApiIgnore | 类,方法,方法参数 |
package com.example.water.controller;
import com.example.water.entity.User;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Api(tags = "控制器说明:用户控制器")
@RestController
public class UserController {
@ApiOperation("接口说明:查询用户接口")
@RequestMapping(value = "/getUser", method = RequestMethod.GET)
public String getUser(@ApiParam(name = "姓名") String name) {
return name;
}
@ApiOperation("接口说明:用户接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "username", value = "用户名", dataType = "String", defaultValue = "dai"),
})
@RequestMapping(value = "/setUser", method = RequestMethod.GET)
@ResponseBody
public User user(@RequestParam(name = "name") String name) {
User user = new User();
user.setName(name);
return user;
}
}
package com.example.water.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("实体类说明:用户实体类")
public class User {
@ApiModelProperty("姓名")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
swagger
http://localhost:8080/swagger-ui.html
Knife4j
http://127.0.0.1:8080/doc.htm
配置与swagger一样,只需要再加个依赖
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
<version>4.4.0</version>
</dependency>