SpringBoot + swagger 简单案例

1.创建Springboot+Mybatis 项目,这里为节省时间,使用了之前写的,链接如下:

SpringBoot Mybatis PageHelper 集成

注:这里可测试数据库连接

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserDaoTest {
	@Autowired
	UserService UserService;
    @Test
    public void getUserList() {
    	List<User> userList = UserService.selectByPage(1,2);
    	JSONObject jsonObject = new JSONObject();
    	jsonObject.put("data", userList);
    	System.out.println(jsonObject.toJSONString());
    }

}

2.pom.xml依赖文件中添加

<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>

3.启动类中添加如下注解

@EnableSwagger2 

4.接口简单修改

@Api(tags = {"用户接口"})
@RestController
public class UserController {
 
	private static Logger logger = LoggerFactory.getLogger(UserController.class);
	@Autowired
	private UserService userService;
	
	@ApiOperation(value = "分页查询用户信息")
	@PostMapping("selectByPage")
	public ModelMap selectByPage(Integer page, Integer rows) {
		logger.info("/user/selectByPage开始调用!");
		ModelMap model = new ModelMap();
		try {
			List<User> userList = userService.selectByPage(page, rows);
			// 获取分页查询后的数据
			PageInfo<User> pageInfo = new PageInfo<User>(userList);
			model.put("data", userList);
			// 总记录数
			model.put("total", pageInfo.getTotal());
			// 当前页
			model.put("page", pageInfo.getPageNum());
		} catch (Exception e) {
			logger.info("/user/selectByPag出现异常!!");
		}
		return model;
	}
}

5.测试

浏览器访问  http://localhost:11111/swagger-ui.html#/

点击User Controller列表,再点击Try it out,填写数据,点击Execute按钮

结果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值