swagger教程-springboot整合swagger
阿新 • • 發佈:2018-12-05
一.swagger簡介
1.swagger是什麼?
swagger是REST APIs介面文件生成工具,既然是REST介面那麼就和Sping Rest 搭上線了,swagger 可以生成一個具有互動性的API控制檯,開發者可以用來快速學習和嘗試API。swagger api控制檯可以更好的方便測試和聯調rest 介面.
二.swagger整合Springboot
1.springboot和Swagger專案結構圖
2.maven 的pom檔案
<?xml version="1.0" encoding="UTF-8"?> <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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.lpinfo.shop</groupId> <artifactId>lpinfo-shop</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>lpinfo-shop</name> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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> </dependencies> <build> <pluginManagement> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.0.0</version> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </pluginManagement> </build> </project>
3.application配置檔案
spring.application.name=lpinfo-shop
server.port=8080
4.swagger配置檔案
package com.lpinfo.shop; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import springfox.documentation.builders.ApiInfoBuilder; 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 springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class Swagger2Configuration { @Bean public Docket buildDoclet() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(buildApiInfo()) .select() //為當前包路徑 .apis(RequestHandlerSelectors.basePackage("com.lpinfo.shop")) .paths(PathSelectors.any()) .build(); } private ApiInfo buildApiInfo() { return new ApiInfoBuilder() //頁面標題 .title("SpringBoot 和 Swagger整合") //建立人 .contact(new Contact("Zhangsan", "http://www.baidu.com", "@qq.com")) //版本號 .version("1.0") //描述 .description("API 描述") .build(); } }
4.app->controller
package com.lpinfo.shop; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.BeanUtils; import org.springframework.web.bind.annotation.*; /** * Hello world! */ @RestController @RequestMapping("/user") public class App { @ApiOperation("新增使用者") @PostMapping("/insertUser") public User insertUser(@RequestBody User user){ user.setUserId(System.currentTimeMillis()); return user; } @ApiOperation("根據Id查詢") @GetMapping("/getUserByUserId/{userId}") public User getUserByUserId(@PathVariable Long userId){ User u = new User(); u.setUserId(userId); return u; } @ApiOperation("根據名稱查詢") @GetMapping("/getUserByUserName") public User getUserByUserName(@RequestParam String userName){ User u = new User(); u.setUserNname(userName); return u; } }
5.啟動類
package com.lpinfo.shop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import java.net.InetAddress;
import java.net.UnknownHostException;
@SpringBootApplication
public class SpringBootMain {
private static final Logger LOG = LoggerFactory.getLogger(SpringBootMain.class.getName());
public static void main(String[] args) {
SpringApplication app = new SpringApplication(SpringBootMain.class);
Environment env = app.run(args).getEnvironment();
LOG.info("\n=========================================================\n\t" +
"應用 '{}' 正在執行!執行地址:\n\t" +
"LOCALURL:\t http://127.0.0.1:{} \n\t" +
"Swagger: \t http://127.0.0.1:{}/swagger-ui.html \n\t" +
"\n=========================================================",
env.getProperty("spring.application.name") + "(V1.1)",
env.getProperty("server.port"),
env.getProperty("server.port"));
}
}
啟動執行
Swagger測試地址===============================
取得結果