1.Swagger简介
说白了就是实时更新api接口,以免前后端工程师打架
2.新建一个springboot(Web)项目
2.1导入maven依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- 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>
2.2新建一个conreoller
package com.yao.swagger.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String hello(){ return "hello"; } }
2.2.1启动SpringbootSwaggerApplication主入口进入检查
(扩展:一个项目只写了一个controller那么这个项目有几个请求,答案为两个,因为无论是否有controller都至少有一个请求那就是error,就是那个报错页面)
2.3配置swagger基本信息
2.3.1创建一个config
package com.yao.swagger.config; import org.springframework.context.annotation.Configuration; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { }//以上含有的注解,是最简单一定要加的至少两个基本注解
2.3.2访问http://localhost:8080/swagger-ui.html测试,会发现直接就有了这个页面,我们明明都没有创建这个页面
又由于swagger有一个bean对象在springboot中
我们接着写它的配置类:
package com.yao.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; import static springfox.documentation.service.ApiInfo.DEFAULT_CONTACT; @Configuration @EnableSwagger2 public class SwaggerConfig { //配置了Swapperbean实例 @Bean public Docket docket(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()); } //配置Swagger信息,就是一些默认的配置信息 private ApiInfo apiInfo(){ //作者信息 Contact contact = new Contact( "幺幺", "https://www.cnblogs.com/yaoyaoo/", "892095368@qq.com"); return new ApiInfo( "幺幺的SwaggerApi", "这个作者很强", "1.0", "https://www.cnblogs.com/yaoyaoo/", contact, //以下三个配置默认 "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList()); } }
未完