swagger教程-springboot整合swagger
一.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测试地址===============================
取得结果