swagger2是什么?
简单来说swagger2是一个可以帮你更好的测试REST风格的API插件
什么是REST风格?
传统的请求URL是http://www.test.com/getUser?userId=1
REST风格的URL是http://www.test.com/getUser/1
百度百科解释
环境
Windows10 家庭版
Spring Boot 2.0
eclipse4.7.3
jdk1.8
spring boot集成swagger2
初始化项目
此次初始化用到了Spring插件,没有插件的直接创建maven工程就好了
首先右键新建项目选择other,然后找到Spring Boot下的Spring Starter Project,选中然后点击next
点击next后请确保电脑网络异常,因为要通过网络加载配置
name处可更改项目名称,在这里我就不改了,继续点击next
然后选中以下几项,如果不需要数据库,可不勾选sql下的组件
然后点击finish
添加swagger2依赖
此时一个Spring Boot项目就算初始化成功了,这时候我们来配置pom.xml
加入swagger2和swagger ui依赖1
2
3
4
5
6
7
8
9
10
11
12<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
编写实体类及配置信息
现在我们来编写一个实体类User:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25package com.example.demo.bean;
public class User {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
编写mapper:1
2
3
4
5
6
7
8
9
10
11
12
13
14package com.example.demo.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.example.demo.bean.User;
public interface UserMapper {
"SELECT * FROM User WHERE id=#{id}") (
User getUser(Integer id);
}
编写controller:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.example.demo.bean.User;
import com.example.demo.mapper.UserMapper;
public class UserController {
private UserMapper userMapper;
"/user/{id}") (
public User getUser(@PathVariable("id") Integer id) {
return userMapper.getUser(id);
}
}
在resource的application.properties中加入数据库配置信息:1
2
3spring.datasource.url=数据库地址
spring.datasource.username=数据库用户名
spring.datasource.password=数据库密码
在测试类中添加一个@EnableSwagger2注解1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
2
public class Demo1Application {
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
}
启动Spring Boot验证是否成功
启动Spring Boot然后访问localhost:8080/swagger-ui.html
此时若出现以下画面即代表启动成功
这时候我们可以测试下,点击user-controller然后再点击GET
测试成功结果: