先让我们用idea创建一个简单的springboot工程
至此环境搭建完成,开始正式代码阶段
首先看一下我的完整的项目结构
第一步,pom文件中引入如下swagger2的依赖
第二步,创建swagger2配置类
package com.example.swagger2demo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @program: swagger2-demo
* @description: swagger2配置类
* @author: Bacy
* @create: 2018年04月24日
**/
@Configuration
@EnableSwagger2
public class swagger2Config {
@Bean
public Docket buildDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())//调用下面apiInfo()方法
.select()
//Controller所在路径
.apis(RequestHandlerSelectors.basePackage("com.example.swagger2demo.web"))
.paths(PathSelectors.any())
.build();
}
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot结合swagger2构建Restful API")
.description("这是一个swagger2小型demo")
.termsOfServiceUrl("www.baidu.com")
.contact("bacyang")
.version("0.0.1")
.build();
}
}
第三步,创建实体类(省略getter,setter)
package com.example.swagger2demo.domain;
import java.io.Serializable;
/**
* @program: swagger2-demo
* @description: 用户测试类
* @author: Bacy
* @create: 2018年04月24日
**/
public class User implements Serializable {
private Long id;
private String name;
private Integer age;
}
第四步,穿件conteroller层
package com.example.swagger2demo.web;
import com.example.swagger2demo.domain.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @program: swagger2-demo
* @description: 用户测试类控制层
* @author: Bacy
* @create: 2018年04月24日
**/
@RestController
@RequestMapping("/users")
public class UserController {
//为了使线程安全,能够接受高并发,这里使用ConcurrentHashMap,小伙伴可以百度一下具体深入一下
static Map map = new ConcurrentHashMap<>();
/**
* @Title: getList
* @Description: 获取用户列表
* @Param: []
* @return: java.util.List
* @Author: Bacy
* @Date: 2018/4/24
*/
@ApiOperation(value = "获取用户列表")
@RequestMapping(value = "",method = RequestMethod.GET)
public List getList() {
List list = new ArrayList<>(map.values());
return list;
}
/**
* @Title: postUser
* @Description: 根据user创建用户
* @Param: [user]
* @return: java.lang.String
* @Author: Bacy
* @Date: 2018/4/24
*/
@ApiOperation(value = "创建用户" , notes = "根据user对象创建用户")
@ApiImplicitParam(name = "user",value = "用户详情实体类",required = true,dataType = "User")
@RequestMapping(value = "",method = RequestMethod.POST)
public String postUser(@RequestBody User user) {
map.put(user.getId(),user);
return "添加成功~~~";
}
/**
* @Title: getUserById
* @Description: 根据用户id获取用户基本信息
* @Param: [id]
* @return: com.example.swagger2demo.domain.User
* @Author: Bacy
* @Date: 2018/4/24
*/
@ApiOperation(value = "获取用户详情",notes = "根据url的id来获取用户基本信息")
@ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "Long",paramType = "path")
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
public User getUserById(@PathVariable Long id) {
return map.get(id);
}
/**
* @Title: putUser
* @Description: 根据用户id来指定更新对象,进行用户的信息更新
* @Param: [id, user]
* @return: java.lang.String
* @Author: Bacy
* @Date: 2018/4/24
*/
@ApiOperation(value = "更新用户信息",notes = "根据url的id来指定对象,并且根据传过来的user进行用户基本信息更新")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "path", dataType = "Long"),
@ApiImplicitParam(name = "user", value = "用户详情实体类user", required = true, dataType = "User")
})
@RequestMapping(value = "/{id}",method = RequestMethod.PUT)
public String putUser(@PathVariable Long id,@RequestBody User user) {
User u = map.get(id);
u.setAge(user.getAge());
u.setName(user.getName());
map.put(id,u);
return "用户基本信息已经更新成功~~~";
}
/**
* @Title: delUser
* @Description: 根据用户id,删除用户
* @Param: [id]
* @return: java.lang.String
* @Author: Bacy
* @Date: 2018/4/24
*/
@ApiOperation(value = "删除用户",notes = "根据url的id来指定对象,进行用户信息删除")
@ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "Long",paramType = "path")
@RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
public String delUser(@PathVariable Long id) {
map.remove(id);
return "用户ID为:"+ id + " 的用户已经被移除系统~~";
}
}
至此项目已经搭建好了,让我们来启动它
idea启动方式:
打开浏览器输入:http://localhost:8888/swagger-ui.html (端口号默认为8080,小伙伴可在application.properties中修改)
可看到如下界面:
和配置类对应关系
点击show/hide可出现controller层你定义的多个方法了
接下来,我来演示一下怎么用(很简单 = = )以post为例,点击进入,
创建user对象(json形式),之后点击左下角Try it out !即可,因为此案例我没有做链接数据库操作,我现在程序中打个断点看一下
发送请求,进入断点,可以看见,值已经传入
swagger2-demo结束,欢迎指正,谢谢~