学习目标
前后端分离
Vue + SrpingBoot
后端时代:前端只用管理静态页面: html ==> 模板引擎 JSP ⇒ 后端是主力
前后端分离时代:
后端:后端控制层,服务层,数据访问层 [后端团队]
前端:前端控制层,视图层 [前端团队]
前后端如何交互? ==> API
前后端相对独立,松耦合;
前后端甚至可以部署在不同的服务器上;
产生一个问题:
解决方案:
在项目中使用Swagger ,需要springfox;
新建一个Springboot项目
1.加入swagger依赖
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
<version>2.9.2version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
<version>2.9.2version>
dependency>
2.编写helloworld
package com.lh.swagger.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello world";
}
}
3.配置Swagger - config
package com.lh.swagger.config;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2 // 开启Swagger2
public class SwaggerConfig {
}
4.测试:主启动加入:@EnableSwagger2:http://localhost:8080/swagger-ui.html
Swagger 的bean的实例 :Docket;
package com.lh.swagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
@Configuration
@EnableSwagger2 // 开启Swagger2
public class SwaggerConfig {
//配置Swagger 的 Docket的bean实例
@Bean
public Docket docket(){
//替换成自己的ApiInfo
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo());
}
//配置Swagger信息 = apiInfo (自定义)
private ApiInfo apiInfo(){
//作者信息
String name = "栾昊";
//指向的url
String url = "https://www.baidu.com/";
//邮箱(收件人)
String email = "[email protected]";
Contact contact = new Contact(name,url, email);
//返回内容
return new ApiInfo(
"栾昊的 Swagger API文档",
"亦如美酒尘封多年",
"1.0",
//服务条款网址
"https://gitee.com/",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<>());
}
页面:
Swagger配置:
//配置Swagger 的 Docket的bean实例
@Bean
public Docket docket() {
//替换成自己的ApiInfo
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
// 通过.select()方法,去配置扫描接口
.select()
//RequestHandlerSelectors,配置要扫描接口的方式
// basePackage:指定要扫描的包
// any:扫描全部
// none:都不扫描
// withClassAnnotation 扫描类上的注解
//withMethodAnnotation 扫描方法上的注解
.apis(RequestHandlerSelectors.basePackage("com.lh.swagger.controller"))
//过滤什么路径
.paths(PathSelectors.ant("/swagger/**"))
.build();
}
@Bean
public Docket docket() {
//替换成自己的ApiInfo
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//不设置为主启动,这样启动就不会启动swagger
.enable(false)
.select()
.apis(RequestHandlerSelectors.basePackage("com.lh.swagger.controller"))
//过滤什么路径
//.paths(PathSelectors.ant("/swagger/**"))
.build();
}
我只希望我的Swagger在生产环境中使用,在发布时候不使用
//配置Swagger 的 Docket的bean实例
@Bean
public Docket docket(Environment environment) {
//设置显示的Swagger环境,只显示dev
Profiles profiles = Profiles.of("dev");
//通过 environment.acceptsProfiles 是否处在自己设置的环境中
boolean flag = environment.acceptsProfiles(profiles);
//替换成自己的ApiInfo
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//设置自己的环境
.enable(flag)
.select()
.apis(RequestHandlerSelectors.basePackage("com.lh.swagger.controller"))
//过滤什么路径
//.paths(PathSelectors.ant("/swagger/**"))
.build();
}
根据API文档的分组:
.groupName("栾昊")
问题:如何配置多个环境?
方式:多个Docket配置多个组
@Bean
public Docket docket1(){
return new Docket(DocumentationType.SWAGGER_2).groupName("A");
}
@Bean
public Docket docket2(){
return new Docket(DocumentationType.SWAGGER_2).groupName("B");
}
package com.lh.swagger.pojo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
//文档注释
@ApiModel("用户实体类")
//@Api(注释)
public class User {
//实体类字段必须用public 显示,不然不会出现字段
//字段注释
@ApiModelProperty("用户名")
public String username;
@ApiModelProperty("密码")
public String password;
}
package com.lh.swagger.controller;
import com.lh.swagger.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.RestController;
@RestController
@Api(description = "hello控制层") //整个类的注释
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello world";
}
//只要我们的接口中返回值出现实体类,他就会被扫描到Swagger中
@PostMapping("/user")
public User user(){
return new User();
}
@ApiOperation("hello控制类") //在方法上加该注释
@GetMapping("/hello2")
//@ApiParam("用户名") 在参数上加注释
public String hello(@ApiParam("用户名") String username){
return "hello world" + username;
}
@ApiOperation("post-hello控制类") //在方法上加该注释
@GetMapping("/postHello")
//@ApiParam("用户名") 在参数上加注释
public User postHello(@ApiParam("用户名") User user){
return user;
}
}
页面:
注:并不是因为@ApiModel这个注解让实体显示在这里了,而是只要出现在接口方法的返回值上的实体都会显示在这里,而@ApiModel和@ApiModelProperty这两个注解只是为实体添加注释的。
@ApiModel为类添加注释
@ApiModelProperty为类属性添加注释
总结:
Swagger的所有注解定义在io.swagger.annotations包下
下面列一些经常用到的,未列举出来的可以另行查阅说明:
Swagger注解 | 简单说明 |
---|---|
@Api(tags = “xxx模块说明”) | 作用在模块类上 |
@ApiOperation(“xxx接口说明”) | 作用在接口方法上 |
@ApiModel(“xxxPOJO说明”) | 作用在模型类上:如VO、BO |
@ApiModelProperty(value = “xxx属性说明”,hidden = true) | 作用在类方法和属性上,hidden设置为true可以隐藏该属性 |
@ApiParam(“xxx参数说明”) | 作用在参数、方法和字段上,类似@ApiModelProperty |
注意点
:在正式发布的时候,关闭Swagger! ! 处于安全考虑,而且节省运行的内存;