如今大都数项目都已经是先后端分离的啦,那么接口文档就成了项目中很是重要的一部分啦,SpringBoot整合Swagger能够自动生成RESTFUL风格的API文档,也能够在其中进行测试,比起之前手写的文档,不只方便不少,并且也易于修改和测试。html
很喜欢一句话:
”八小时内谋生活,八小时外谋发展“
前端
咱们:"待别日相见时,都已有所成”
😁java
好的天气,好的心情
程序员
如今小伙伴学习SpringBoot大都数是先后端开发的,这个API接口文档真的不可缺乏的一部分。web
咱们开发好项目-->启动-->测试-->前端查看API文档-->数据渲染。用Swagger能够不用写本身写了,能够直接在代码中声明,很是方便,也易于更改。 我这个东东能够直接CV哈,没啥特殊的,直接能够跑起来滴。😁😁😁spring
Swagger 是一个用于生成、描述和调用 RESTful 接口的 Web 服务。通俗的来说,Swagger 就是将项目中全部(想要暴露的)接口展示在页面上,而且能够进行接口调用和测试的服务。apache
案例:后端
写一个RESTFUL风格的增删改查哈,而后展现接口哈。api
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
复制代码
server:
port: 8088
spring:
application:
name: springboot-swagger
swagger:
enable: true
复制代码
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
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;
/** * @version 1.0 * @author: crush * @date: 2020-12-01 10:48 * @EnableSwagger2 启动使用Swagger2 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
// 经过配置文件中这个变量的值来开启或关闭
@Value("${swagger.enable}")
private Boolean enable;
@Bean
public Docket docket(Environment environment) {
return new Docket(DocumentationType.SWAGGER_2)
.enable(enable)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.crush.swagger"))
.build();
}
public ApiInfo apiInfo() {
// 这里是做者信息及文档的基本信息 和页面展现信息一一对照便可
Contact contact = new Contact("springboot-swagger ", "https://blog.csdn.net/weixin_45821811?spm=1011.2124.3001.5343", "951930136@qq.com");
return new ApiInfo(
"springboot-swagger Demo API接口文档",
"此处填写描述信息",
"1.0",
"urn:tos",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList()
);
}
}
复制代码
@Data
@Accessors(chain = true)
@ApiModel(description = "帐户相关信息类")
public class Account {
@ApiModelProperty("帐号")
private String username;
@ApiModelProperty("密码")
private String password;
}
复制代码
public interface AccountService {
/** * 注册 * @param account * @return */
boolean register(Account account);
/** * 查询所有 */
List<Account> select();
}
复制代码
impl:我此处只是用了静态变量模拟了一下~~(太懒啦捂脸)~~springboot
@Service
public class AccountServiceImpl implements AccountService {
private static List<Account> accountList = new ArrayList<Account>();
@Override
public boolean register(Account account) {
accountList.add(account);
return true;
}
@Override
public List<Account> select() {
return accountList;
}
}
复制代码
@Api(tags = "帐户相关接口")
@RestController
@RequestMapping("/account")
public class AccountController {
private final AccountService accountService;
public AccountController(AccountService accountService) {
this.accountService = accountService;
}
@ApiOperation("注册接口")
@PostMapping("/register")
public String register(@RequestBody @ApiParam(required = true, value = "注册帐户须要的信息") Account account) {
accountService.register(account);
return "OK";
}
@ApiOperation("查询所有")
@GetMapping
public List<Account> select() {
return accountService.select();
}
}
复制代码
启动类就是普普统统的没啥特殊的,让咱们直接开始吧。
@Slf4j
@SpringBootApplication
public class SpringBootSwagger {
public static void main(String[] args) {
SpringApplication.run(SpringBootSwagger.class);
log.info("API接口访问连接:http://localhost:8088/swagger-ui.html");
}
}
复制代码
启动以后输入:就能看到页面
http://localhost:8088/swagger-ui.html
复制代码
以前配置的相关信息就也会所有展现在上面。
咱们点开接口看一下
而后咱们经过咱们的查询接口也可以查询到了
完事啦,摸鱼啦摸鱼啦👨💻
Swagger的优势:
缺点:
Postaman的优势:
缺点我我没杂感受到。
结论:对于咱们来说,不管是Swagger和PostMan都是须要掌握的,这是最基本最基本的要求。
你好,我是博主宁在春
😁
若是你看到这篇文章,而且以为对你有益的话,就给个赞吧,让我感觉一下分享的喜悦吧,蟹蟹。🤗
如如有写的有误的地方,也请你们不啬赐教!!
一样如如有存在疑惑的地方,请留言或私信,定会在第一时间回复你。
持续更新中