Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。
- 使得前后端分离开发更加方便,有利于团队协作。(实际开发中,接口文档的内容会不停的发生变化,如果没有及时更新接口文档,那么前后端就不能及时同步信息。我们通过在线的接口文档swagger,这样前后端工程师都遵守swagger就行了,只要接口文档发生了变化,就会实时更新。)
- 接口的文档在线自动生成,降低后端开发人员编写接口文档的负担
- 功能测试
Spring已经将Swagger纳入自身的标准,建立了Spring-swagger项目,现在叫Springfox。通过在项目中引入Springfox ,即可非常简单快捷的使用Swagger
实验目的:接口的文档在线自动生成
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springboot-demo</artifactId><groupId>com.et</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>swagger</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies></project>
package com.et.swagger.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import springfox.documentation.builders.PathSelectors;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 java.util.Collections;@Configurationpublic class SwaggerConfiguration {private ApiInfo apiInfo() {return new ApiInfo("Blog REST APIs","REST APIs for Blog Application","1.0","http://www.liuhaihua.cn",new Contact("HBLOG", "http://www.liuhaihua.cn", "xxx"),"License of API","API license URL",Collections.emptyList());}@Beanpublic Docket api() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build();}}
package com.et.swagger.controller;import com.et.swagger.model.LoginDto;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import org.springframework.web.bind.annotation.*;import java.util.HashMap;import java.util.Map;public class HelloWorldController {public Map<String, Object> showHelloWorld(){Map<String, Object> map = new HashMap<>();map.put("msg", "HelloWorld");LoginDto loginDto = new LoginDto();loginDto.setPassword("");loginDto.setPhone("");map.put("loginuser", loginDto);return map;}public LoginDto login( LoginDto loginDto){return loginDto;}}
package com.et.swagger.model;import io.swagger.annotations.ApiModelProperty;import lombok.Data;@Datapublic class LoginDto {@ApiModelProperty(value = "phone",required = true)private String phone;@ApiModelProperty(value = "password",required = true)private String password;}
以上只是一些关键代码,所有代码请参见下面代码仓库
- https://github.com/Harries/springboot-demo
启动Spring Boot应用
- http://localhost:8088/v3/api-docs
- http://127.0.0.1:8088/swagger-ui/


- https://swagger.io/
- http://www.liuhaihua.cn/archives/710497.html
版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/rfx/72888.html