springboot的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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
添加swaggerconfig的配置类
当然这里可以直接在配置文件里面添加配置.我这里直接添加 了一个配置类,首先通过@EnableSwagger2注解启动swagger2,然后配置一个Docket bean,配置映射路径和要扫描的接口的位置,在apiInfo中,主要配置一下Swagger2文档网站的信息,例如网站的title,网站的描述,联系人的信息,使用的协议等等。
/**
* @Classname SwaggerConfig
* @Description swagger
* @Date 2020/5/22 8:47
* @Created by corey
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.basePackage("com.nvn.controller"))
.paths(PathSelectors.any())
.build().apiInfo(new ApiInfoBuilder()
.title("SpringBoot整合Swagger")
.description("SpringBoot整合Swagger,详细信息......")
.version("9.0")
.contact(new Contact("corey", "https://blog.csdn.net/CoreyXuu", "coreyxuyong@163.com"))
.license("The Apache License")
.licenseUrl("https://blog.csdn.net/CoreyXuu")
.build());
}
}
配置完成后启动项目
地址栏输入:http://localhost:8080/swagger-ui.html
项目启动很顺利
swagger项目接口管理配置
这里简单说明下swagger的注解
- @Api注解可以用来标记当前Controller的功能。
- @ApiOperation注解用来标记一个方法的作用。
- @ApiImplicitParam注解用来描述一个参数,可以配置参数的中文含义,也可以给参数设置默认值,这样在接口测试的时候可以避免手动输入。
- 如果有多个参数,则需要使用多个@ApiImplicitParam注解来描述,多个@ApiImplicitParam注解需要放在一个@ApiImplicitParams注解中。
- 需要注意的是,@ApiImplicitParam注解中虽然可以指定参数是必填的,但是却不能代替@RequestParam(required = true),前者的必填只是在Swagger2框架内必填,抛弃了Swagger2,这个限制就没用了,所以假如开发者需要指定一个参数必填,@RequestParam(required = true)注解还是不能省略。
- 如果参数是一个对象@ApiModel
加上注解后启动项目
返回参数:
代码上传到 https://github.com/coreyxuy/springboot-email.git