swagger的使用
可以声明
在SwaggerConfig类中可以声明多个Docket方法
每个Docket方法可以扫描一个
basePackage:指定要扫描的包
方便开发
package com.lishao.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.RequestHandler;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
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
@EnableOpenApi //开启Swagger2
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");
}
@Bean
public Docket docket(Environment environment){
//设置要显示的swagger环境
Profiles profiles = Profiles.of("dev");
//通过environment。acceptsProfiles判断是否处在自己的设定的环境中
boolean b = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(b)
.groupName("李少")
.select()
/**
* RequestHandlerSelectors,配置要扫描接口的方式
* basePackage:指定要扫描的包
* any:扫描全部
* none:不扫描
* withClassAnnotation:扫描类上的注解,参数是一个注解的反射对象
* withMethodAnnotation:扫描方法上的注解
*/
.apis(RequestHandlerSelectors.basePackage("com.lishao.controller"))
//paths 过滤(忽略)什么路径
//.paths(PathSelectors.ant("/lishao/**"))
.build();
}
//配置swagger 信息apiInfo
private ApiInfo apiInfo(){
//作者信息
Contact contact = new Contact("李少","http://localhost:8080/","854549294@qq.com");
return new ApiInfo("李少的SwaggerApi日记",
"即使再小的帆也能远航",
"VV1.0",
"www.baidu.com",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
就可以看到
所以这个 李少 能被重写
这个Model就是User的实体类 可以在swagger中查看
下面是controller类中的方法
package com.lishao.controller;
import com.lishao.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Api(tags = "用户相关")
@RestController
public class HelloController {
@GetMapping("/")
public String hello(){
return "hello";
}
@PostMapping("/user")
public User user(){
return new User();
}
//ApiOperation放在方法上 不是放在类上
@ApiOperation("Hello控制类")
@GetMapping(value = "/hello2")
public String hello2(@ApiParam("用户名") String username){
return "hello"+username;
}
@PostMapping(value = "/postt")
public User postt( User user){
return user;
}
}
就可以传递参数 进行测试