引入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
配置
@Configuration
@EnableOpenApi
public class SwaggerConfig {
/** 获取当前环境 */
@Value("${spring.profiles.active}")
private String env;
/** Swagger的Docket的Bean实例 */
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.enable(Objects.equals(env, "dev"))//仅在开发环境开启swagger
.select()
//配置要扫描的包
.apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
//配置要扫描的路径
.paths(PathSelectors.ant("/site/**"))
.build()
.groupName("A");//设置组名
}
@Bean
public Docket docket1(){
return new Docket(DocumentationType.SWAGGER_2).groupName("B");//开启第二个组
}
/** Docket的apiInfo属性 */
private ApiInfo apiInfo() {
return new ApiInfo("我的Swagger接口文档", "我的Swagger接口描述", "V0.1", "http://mysite.com", new Contact("name", "url", "email"), "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
}
}
使用
@RestController
@RequestMapping("site")
@Api(tags = "SiteController 说明")// 接口类说明
public class SiteController {
@GetMapping("index")
@ApiOperation("index 说明")// 方法说明
public String index(@ApiParam("用户名") String username){ // 参数说明
return "Hello "+username;
}
}
@Data
@ApiModel("用户实体类")//实体类说明,若想在model中显示,接口中只要用到这个实体类就可以了
public class User {
@ApiModelProperty("用户名")//属性说明
private String username;
private Integer age;
}
接口访问路径
http://localhost:8080/swagger-ui/
标签:return,String,new,Swagger,Docket,public From: https://www.cnblogs.com/Bin-x/p/16853232.html