swagger简介
前后端分离
目前最流行的前后端分离框架:
Vue+Springboot
后端时代:前段只管理静态样式;html–>后端。模版引擎JSP=>后端是主力
前后端分离时代:
- 后端:后端控制层,服务层,数据访问层
- 前端: 前端控制层,视图层
伪造后端数据,json,已经存在了,一般数据都是通过api接口从后端拿的。
核心:前后端交互主要靠api
好处:前后端相对分离,松耦合,前后端甚至可以部署在不同的服务器上
产生的问题:
- 前后端集成联调的时候,前后端开发人员无法做到“即时协商,尽早解决”,导致问题集中爆发。
解决方案:
- 首先指定schema【计划的提纲】,实时更新API,降低集成的风险
- 早些时候使用word指定计划文档
- 前后端分离:
- 前端测试后端接口:postman
- 后端提供,需要实时更新最新的消息及改动
Swagger
- 号称世界上最流行的api框架;
- RestFul Api 文档在线自动生成工具=>Api文档与API定义自动同步更新
- 直接运行,可以直接运行api
- 支持多种语言(java,api)
在项目中使用Swagger需要springfox:
- swagger2
- ui
springboot集成swagger
- 新建一个springboot项目–选择web项目
- 先去导入两个必须的包
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
-
编写一个helloworld
-
配置swagger==>config
package com.mao.swagger.config;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
}
-
测试运行:
-
配置代码
package com.mao.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.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(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
}
//配置swagger信息=aipInfo
private ApiInfo apiInfo(){
//作者信息
Contact contact = new Contact("Mao yz", "https://www.baidu.com", "[email protected]");
return new ApiInfo("Wuwei的swagger文档",
"徐少卿我最爱",
"1.0",
"http://www.baidu.com",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<>());
}
}
效果图:
配置扫描接口及开关
package com.mao.swagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
//配置了Swagger的Docket的bean实例
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//.enable()是否启动swagger,若果是false,则不能在浏览器中访问
.select()
//RequestHandlerSelectors:配置要扫描接口的方式
//basePackage:指定要扫描的包
//any():扫描全部
//none()不扫描;
//withClassAnnotation:扫描类下注解
//withMethodAnnotation:扫描方法注解
//paths:过滤指定的路径
.apis(RequestHandlerSelectors.basePackage("com.mao.swagger.controller"))
.build();
}
//配置swagger信息=aipInfo
private ApiInfo apiInfo(){
//作者信息
Contact contact = new Contact("Mao yz", "https://www.baidu.com", "[email protected]");
return new ApiInfo("Wuwei的swagger文档",
"徐少卿我最爱",
"1.0",
"http://www.baidu.com",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<>());
}
}
通过上面的配置我们可以指定要扫描的路径,还可以指定swagger是否开启。
注释的使用
package com.mao.swagger.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("用户实体类")
public class User {
@ApiModelProperty("用户名")
private String username;
@ApiModelProperty("密码")
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.mao.swagger.controller;
import com.mao.swagger.pojo.User;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String sayHello(){
return "helloworld";
}
//只要我们在接口中,返回值中存在实体类,他就会被扫描到swagger中
@ApiOperation("user接口")
@PostMapping(value = "/user")
public User user(){
return new User();
}
}
上面是实体类和Controller的代码,要知道swagger中控制器中只有在接口返回值中存在的实体类才会被扫描到swagger中,而不是说实体类中配置了就会被扫描进去。
效果: