用于前后端的分离,可以实时的展示后端的接口
一般Springboot配置,都会有一个config类,用于书写配置(config配置)
SwaggerConfig
package com.mystudy.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
@Configuration
//开启Swagger2
@EnableSwagger2
public class SwaggerConfig {
//分组问题
@Bean
public Docket docket1(){
return new Docket((DocumentationType.SWAGGER_2)).groupName("A");
}
@Bean
public Docket docket2(){
return new Docket((DocumentationType.SWAGGER_2)).groupName("B");
}
@Bean
public Docket docket3(){
return new Docket((DocumentationType.SWAGGER_2)).groupName("C");
}
//配置Swagger的Docket的bean实例
@Bean //设置开发时不开启swagger,而生产时开启swagger
public Docket docket(Environment environment){
//设置要显示的Swagger环境
Profiles profiles = Profiles.of("dev","test");
//获取当前项目的环境,通过environment.acceptsProfiles(profiles)判断是否处在自己设定的环境当中
boolean flag = environment.acceptsProfiles(profiles);
//1.配置swagger信息
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//enable()是否启用swagger,如果为false,则Swagger不能在浏览器中访问(用于开发环境和生产环境是否开启swagger)
//.enable(flag)
//设置分组的名字
.groupName("myStudy")
.select()
//requestHandler->表示的为requestHandler的接口,要写实现类 或者自定义实现类
//则这里使用它的RequestHandlerSelectors实现类
//RequestHandlerSelectors:配置要扫描接口的方式
//basePackage():扫描指定目录下的包
//any():扫描全部的包
//none():都不扫描
//withClassAnnotation():扫描类上的注解,参数是一个注解的反射对象
//withMethodAnnotation(): 扫描方法上的注解
.apis(RequestHandlerSelectors.basePackage("com.mystudy.controller"))
//过滤路径(只扫描请求中带有mystudy下的请求的方法)
//.paths(PathSelectors.ant("/mystudy/**"))
.build(); //build
}
//配置Swagger信息(APIInfo)
private ApiInfo apiInfo(){
//作者信息
Contact contact = new Contact("朱润泽", "http://localhost:8080", "626243980@qq.com");
return new ApiInfo("MyStudy Swagger接口日志",
"三年不飞一飞冲天,三年不鸣一鸣惊人",
"v1.0",
"http://localhost:8080",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
实体类的书写(要加Swagger上面的注解)
//@ApiModel()用于Swagger展示时的文档注释
@ApiModel("用户实体类")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
//@ApiModelProperty("用户名")给生成的文档加入注释
@ApiModelProperty("用户名")
private String username;
@ApiModelProperty("密码")
private String password;
}
Controller上的书写
@RestController
public class HelloController {
//还有一个请求时error
@GetMapping("/hello")
public String hello(){
return "hello,Spring";
}
//只要我们的接口中,返回值中存在实体类,他就会被扫描到Swagger中
@PostMapping("/user")
public User user(){
return new User();
}
@ApiOperation("Hello控制类")
@GetMapping("/hello2")
public String hello2(@ApiParam("用户名") String username){
return "hello"+username;
}
@ApiOperation("Post测试")
@GetMapping("/postTest")
public User postTest(@ApiParam("用户") User user){
return user;
}
}