POM依赖:
<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配置类:
package com.lsr.swagger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
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.EnableSwagger2;
/**
* @Description : 配置类
* @program : demo
* @Package : com.lsr.demo.swagger
* @Author : Hacker_lsr@126.com
* @Version : 1.0
*/
@Configuration
@EnableSwagger2
@ConfigurationProperties(prefix = "swagger")
public class SwaggerConfig {
@Value("${swagger.enable}")
private boolean enable;
@Value("${swagger.controller}")
private String controller;
@Value("${swagger.title}")
private String title;
@Value("${swagger.description}")
private String description;
@Value("${swagger.version}")
private String version;
@Value("${swagger.license}")
private String license;
@Value("${swagger.licenseUrl}")
private String licenseUrl;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(enable)
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.basePackage(controller))
.paths(PathSelectors.any())
.build().apiInfo(new ApiInfoBuilder()
.title(title)
.description(description)
.version(version)
.license(license)
.licenseUrl(licenseUrl)
.build());
}
}
application.properties
# swagger配置
#开关 swagger.enable=false
#扫描controller 类包路径 swagger.controller=com.lsr.controller
#标题 swagger.title=Terry Api 测试
#描述 swagger.description=开始测试吧........
#版本 swagger.version=9.2
#外连接 swagger.license=博客
swagger.licenseUrl=https://www.cnblogs.com/hacker-lsr/
测试Controller(注解的使用)
package com.lsr.controller;
import com.lsr.entity.BaseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.management.Agent;
/**
* @Description : 测试丝袜哥
* @program : demo
* @Package : com.lsr.controller
* @Author : Hacker_lsr@126.com
* @Version : 1.0
*/
@RestController
@Api(tags = "测试丝袜哥")
@RequestMapping("/test")
public class TestController {
@PostMapping("/add")
@ApiOperation("添加用户信息")
@ApiImplicitParams({@ApiImplicitParam(name ="name",value = "名字",defaultValue = "测试")})
public BaseEntity insert(String name ,String age){
return new BaseEntity();
}
}
SwaggerUI

点击 try it out

输入参数执行Excute

成功:

浙公网安备 33010602011771号