架构实战篇(三)-Spring Boot架构搭建RESTful API案例
2018-02-08 14:23
453 查看
前言
上一篇分享了Spring Boot 整合Swagger 让API可视化和前后端分离架构 受到了大家一致好评 ,本节就接着上节的代码做了详细的查询代码的补充和完善并搭建RESTful API架构案例。注意: 本节代码都是在上节代码的基础上追加的内容,如果需要源码的可以去看上节内容或者关注我们的公众号,回复 MyBatis(二) 和你的邮箱地址,我们发送给你。
点我!传送到上节 - Spring Boot 整合MyBatis
我们先看下已经完成的项目结构图
如果看过上节文章的同学应该注意到Code 和 Result 怎么没有了?那是因为为了节省篇幅我们用Spring提供的ResponseEntity 类代替了
最终运行结果
下面开始一步一步的编写代码了
增加Spring boot的maven 依赖
在原有基础的pom结构中追加Swagger2的依赖<!-- swagger2--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
增加一个查询用的Form类
里面使用到了swagger 的注解import io.swagger.annotations.ApiModelProperty; import java.io.Serializable; import java.util.List; public class HelpCategoryForm implements Serializable { // 查询条件 @ApiModelProperty(value = "分类编号集合") private List<Integer> idList; @ApiModelProperty(value = "模糊查询分类名称") private String nameLike; @ApiModelProperty(value = "父类编号") private Integer parentId; // 分页 @ApiModelProperty(value = "第几页", example = "1") private Integer pageNum; @ApiModelProperty(value = "显示行数", example = "10") private Integer pageSize; // 排序 @ApiModelProperty(value = "排序", allowableValues = "help_category_id,name", notes = "(后面空格追加 升序ASC, 降序DESC), 例如: name asc 或者 name desc") private String orderBy; // 省略了get set 方法 @Override public String toString() { return "HelpCategoryForm{" + "idList=" + idList + ", nameLike='" + nameLike + '\'' + ", parentId=" + parentId + ", orderBy='" + orderBy + '\'' + ", pageNum=" + pageNum + ", pageSize=" + pageSize + '}'; } }
增加两个查询语句
<select id="selectList" resultMap="BaseResultMap" parameterType="com.example.model.HelpCategoryForm"> select help_category_id, name, parent_category_id, url from help_category <where> <if test="idList != null"> and help_category_id in <foreach collection="idList" open="(" separator="," close=")" item="id"> #{id} </foreach> </if> <if test="nameLike != null"> and name like concat(#{nameLike}, '%') </if> <if test="parentId != null"> and parent_category_id = #{parentId} </if> </where> </select> <select id="selectById" resultMap="BaseResultMap" parameterType="int"> select help_category_id, name, parent_category_id, url from help_category WHERE help_category_id = #{id} </select>
Mapper 的修改
import d231 com.example.model.HelpCategory; import com.example.model.HelpCategoryForm; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface HelpCategoryMapper { List<HelpCategory> selectList(HelpCategoryForm form); HelpCategory selectById(Integer id); }
编写服务层代码
这边用到了github的pagehelper工具类,他会去找你的类中有没有pageNum,pageSize,orderBy属性,所以命名上不要自己太个性化了import com.example.model.HelpCategory; import com.example.mapper.HelpCategoryMapper; import com.example.model.HelpCategoryForm; import com.example.service.HelpCategoryService; import com.github.pagehelper.PageHelper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class HelpCategoryServiceImpl implements HelpCategoryService { @Autowired private HelpCategoryMapper helpCategoryMapper; @Override public List<HelpCategory> selectList(HelpCategoryForm form) { PageHelper.startPage(form); return helpCategoryMapper.selectList(form); } @Override public HelpCategory selectById(Integer id) { return helpCategoryMapper.selectById(id); } }
API 接口
import com.example.model.HelpCategory; import com.example.model.HelpCategoryForm; import com.example.service.HelpCategoryService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; @Api(value = "帮助类别", description = "帮助类别") @RestController @RequestMapping("help/category") public class HelpCategoryController { @Autowired private HelpCategoryService helpCategoryService; // 注意:GET 请求不支持 RequestBody 注解 @ApiOperation("列表查询") @RequestMapping(value = "/", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") public ResponseEntity<List<HelpCategory>> list(@RequestBody HelpCategoryForm form) { List<HelpCategory> list = helpCategoryService.selectList(form); return ResponseEntity.ok(list); } @ApiOperation("详情查询") @ApiResponses({ @ApiResponse(code = 404, message = "分类不存在", response = Void.class) }) @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8") public ResponseEntity<HelpCategory> detail(@PathVariable("id") Integer id) { HelpCategory category = helpCategoryService.selectById(id); // 如果分类不存在返回编码 404 if(category == null) return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); return ResponseEntity.ok(category); } }
注意:GET 请求不支持 RequestBody 注解
程序的入口处有所调整,增加了一个api入口方便访问
import io.swagger.annotations.ApiOperation; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @SpringBootApplication @RestController public class Application { @ApiOperation("Hello World") @RequestMapping(value = "/", method = RequestMethod.GET) public String index() { return "Hello Spring Boot"; } @ApiOperation("API接口") @RequestMapping(value = "/api", method = RequestMethod.GET) public void api(HttpServletResponse response) throws IOException { response.sendRedirect("swagger-ui.html"); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
到这里所有的类都编写完了,好了见证奇迹的时候到了
让我们打开浏览器地址栏访问
http://localhost:8081/demo/api
测试列表查询
点击Try it out!
{ "idList": [ 1,2,3,4,5,6,7,8,9,10,11,12 ], "nameLike": "P", "orderBy": "help_category_id desc", "pageNum": 1, "pageSize": 10, "parentId": 33 }
查看下结果
测试详情查询
点击Try it out!
查询结果
测试下不存在的分类
输入一个不存在的分类编号:1111111 点击查询返回404说明成功
你的运行结果对了吗?
更多精彩内容请关注”IT实战联盟” 公众号哦~~~
相关文章推荐
- QiYu Admin-架构搭建(SpringBoot实战)
- Spring Boot 和 Spring Cloud 实战 - 从无到有搭建信息管理系统(系统总体架构) -- TerryHe 博客园
- QiYuAdmin-架构搭建一(SpringBoot实战)
- 架构实战项目心得(七):使用SpringBoot+Dubbo+Mybatisplus+Oracle搭建后台项目框架(二)
- 架构实战项目心得(七):使用SpringBoot+Dubbo+Mybatisplus+Oracle搭建后台项目框架(一)
- 教你十分钟快速搭建springBoot项目实战
- 基于springboot+bootstrap+mysql+redis搭建一套完整的权限架构【五】【编写基础代码快速生成工具】
- SpringBoot实战_1 Spring项目搭建
- SpringBoot(一)_快速实战搭建项目
- Spring Cloud Spring Boot mybatis分布式微服务云架构(九)使用Swagger2构建强大的RESTful API文档(1)
- 基于springboot+bootstrap+mysql+redis搭建一套完整的权限架构【二】【整合springSecurity】
- 基于springboot+bootstrap+mysql+redis搭建一套完整的权限架构【四】【编写基础开发工具】
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十)使用Swagger2构建强大的RESTful API文档(2)
- springboot快速实战搭建篇
- springboot手把手教程实战-环境搭建-001
- spring-boot实战【01】:Hello World项目搭建
- Spring Cloud搭建微服务架构----使用Spring boot开发web项目
- spring boot 集成 zookeeper 搭建微服务架构
- springboot+Rabit实战一:(Rabbit MQ windows 环境搭建)
- springboot+Rabit实战三:(springboot+rabbit 项目搭建)