1. 前言

今天开始搭建我们的kono Spring Boot脚手架,首先会集成Spring MVC并进行定制化以满足日常开发的需要,我们先做一些刚性的需求定制,后续再补充细节。如果你看了本文有什么问题可以留言讨论。多多持续关注,共同学习,共同进步。

Gitee: https://gitee.com/felord/kono

GitHub: https://github.com/NotFound403/kono

2. 统一返回体

在开发中统一返回数据非常重要。方便前端统一处理。通常设计为以下结构:

  1. {
  2. "code": 200,
  3. "data": {
  4. "name": "felord.cn",
  5. "age": 18
  6. },
  7. "msg": "",
  8. "identifier": ""
  9. }
  • code 业务状态码,设计时应该区别于http状态码。
  • data 数据载体,用以装载返回给前端展现的数据。
  • msg 提示信息,用于前端调用后返回的提示信息,例如 “新增成功”、“删除失败”。
  • identifier 预留的标识位,作为一些业务的处理标识。

根据上面的一些定义,声明了一个统一返回体对象RestBody<T>并声明了一些静态方法来方便定义。

  1. package cn.felord.kono.advice;
  2. import lombok.Data;
  3. import java.io.Serializable;
  4. /**
  5. * @author felord.cn
  6. * @since 22:32 2019-04-02
  7. */
  8. @Data
  9. public class RestBody<T> implements Rest<T>, Serializable {
  10. private static final long serialVersionUID = -7616216747521482608L;
  11. private int code = 200;
  12. private T data;
  13. private String msg = "";
  14. private String identifier = "";
  15. public static Rest<?> ok() {
  16. return new RestBody<>();
  17. }
  18. public static Rest<?> ok(String msg) {
  19. Rest<?> restBody = new RestBody<>();
  20. restBody.setMsg(msg);
  21. return restBody;
  22. }
  23. public static <T> Rest<T> okData(T data) {
  24. Rest<T> restBody = new RestBody<>();
  25. restBody.setData(data);
  26. return restBody;
  27. }
  28. public static <T> Rest<T> okData(T data, String msg) {
  29. Rest<T> restBody = new RestBody<>();
  30. restBody.setData(data);
  31. restBody.setMsg(msg);
  32. return restBody;
  33. }
  34. public static <T> Rest<T> build(int code, T data, String msg, String identifier) {
  35. Rest<T> restBody = new RestBody<>();
  36. restBody.setCode(code);
  37. restBody.setData(data);
  38. restBody.setMsg(msg);
  39. restBody.setIdentifier(identifier);
  40. return restBody;
  41. }
  42. public static Rest<?> failure(String msg, String identifier) {
  43. Rest<?> restBody = new RestBody<>();
  44. restBody.setMsg(msg);
  45. restBody.setIdentifier(identifier);
  46. return restBody;
  47. }
  48. public static Rest<?> failure(int httpStatus, String msg ) {
  49. Rest<?> restBody = new RestBody< >();
  50. restBody.setCode(httpStatus);
  51. restBody.setMsg(msg);
  52. restBody.setIdentifier("-9999");
  53. return restBody;
  54. }
  55. public static <T> Rest<T> failureData(T data, String msg, String identifier) {
  56. Rest<T> restBody = new RestBody<>();
  57. restBody.setIdentifier(identifier);
  58. restBody.setData(data);
  59. restBody.setMsg(msg);
  60. return restBody;
  61. }
  62. @Override
  63. public String toString() {
  64. return "{" +
  65. "code:" + code +
  66. ", data:" + data +
  67. ", msg:" + msg +
  68. ", identifier:" + identifier +
  69. '}';
  70. }
  71. }

但是每次都要显式声明返回体也不是很优雅的办法,所以我们希望无感知的来实现这个功能。Spring Framework正好提供此功能,我们借助于@RestControllerAdviceResponseBodyAdvice<T>来对项目的每一个@RestController标记的控制类的响应体进行后置切面通知处理。

  1. /**
  2. * 统一返回体包装器
  3. *
  4. * @author felord.cn
  5. * @since 14:58
  6. **/
  7. @RestControllerAdvice
  8. public class RestBodyAdvice implements ResponseBodyAdvice<Object> {
  9. @Override
  10. public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
  11. return true;
  12. }
  13. @Override
  14. public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
  15. // 如果为空 返回一个不带数据的空返回体
  16. if (o == null) {
  17. return RestBody.ok();
  18. }
  19. // 如果 RestBody 的 父类 是 返回值的父类型 直接返回
  20. // 方便我们可以在接口方法中直接返回RestBody
  21. if (Rest.class.isAssignableFrom(o.getClass())) {
  22. return o;
  23. }
  24. // 进行统一的返回体封装
  25. return RestBody.okData(o);
  26. }
  27. }

当我们接口返回一个实体类时会自动封装到统一返回体RestBody<T>中。

既然有ResponseBodyAdvice,就有一个RequestBodyAdvice,它似乎是来进行前置处理的,以后可能有一些用途。

2. 统一异常处理

统一异常也是@RestControllerAdvice能实现的,可参考之前的Hibernate Validator校验参数全攻略。这里初步集成了校验异常的处理,后续会添加其他异常。

  1. /**
  2. * 统一异常处理
  3. *
  4. * @author felord.cn
  5. * @since 13 :31 2019-04-11
  6. */
  7. @Slf4j
  8. @RestControllerAdvice
  9. public class ApiExceptionHandleAdvice {
  10. @ExceptionHandler(BindException.class)
  11. public Rest<?> handle(HttpServletRequest request, BindException e) {
  12. logger(request, e);
  13. List<ObjectError> allErrors = e.getAllErrors();
  14. ObjectError objectError = allErrors.get(0);
  15. return RestBody.failure(700, objectError.getDefaultMessage());
  16. }
  17. @ExceptionHandler(MethodArgumentNotValidException.class)
  18. public Rest<?> handle(HttpServletRequest request, MethodArgumentNotValidException e) {
  19. logger(request, e);
  20. List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
  21. ObjectError objectError = allErrors.get(0);
  22. return RestBody.failure(700, objectError.getDefaultMessage());
  23. }
  24. @ExceptionHandler(ConstraintViolationException.class)
  25. public Rest<?> handle(HttpServletRequest request, ConstraintViolationException e) {
  26. logger(request, e);
  27. Optional<ConstraintViolation<?>> first = e.getConstraintViolations().stream().findFirst();
  28. String message = first.isPresent() ? first.get().getMessage() : "";
  29. return RestBody.failure(700, message);
  30. }
  31. @ExceptionHandler(Exception.class)
  32. public Rest<?> handle(HttpServletRequest request, Exception e) {
  33. logger(request, e);
  34. return RestBody.failure(700, e.getMessage());
  35. }
  36. private void logger(HttpServletRequest request, Exception e) {
  37. String contentType = request.getHeader("Content-Type");
  38. log.error("统一异常处理 uri: {} content-type: {} exception: {}", request.getRequestURI(), contentType, e.toString());
  39. }
  40. }

3. 简化类型转换

简化Java Bean之间转换也是一个必要的功能。 这里选择mapStruct,类型安全而且容易使用,比那些BeanUtil要好用的多。但是从我使用的经验上来看,不要使用mapStruct提供的复杂功能只做简单映射。详细可参考文章Spring Boot 2 实战:集成 MapStruct 类型转换

集成进来非常简单,由于它只在编译期生效所以引用时的scope最好设置为compile,我们在kono-dependencies中加入其依赖管理:

  1. <dependency>
  2. <groupId>org.mapstruct</groupId>
  3. <artifactId>mapstruct</artifactId>
  4. <version>${mapstruct.version}</version>
  5. <scope>compile</scope>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.mapstruct</groupId>
  9. <artifactId>mapstruct-processor</artifactId>
  10. <version>${mapstruct.version}</version>
  11. <scope>compile</scope>
  12. </dependency>

kono-app中直接引用上面两个依赖,但是这样还不行,和lombok一起使用编译容易出现SPI错误。我们还需要集成相关的Maven插件到kono-app编译的生命周期中去。参考如下:

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-compiler-plugin</artifactId>
  4. <version>3.8.1</version>
  5. <configuration>
  6. <source>1.8</source>
  7. <target>1.8</target>
  8. <showWarnings>true</showWarnings>
  9. <annotationProcessorPaths>
  10. <path>
  11. <groupId>org.projectlombok</groupId>
  12. <artifactId>lombok</artifactId>
  13. <version>${lombok.version}</version>
  14. </path>
  15. <path>
  16. <groupId>org.mapstruct</groupId>
  17. <artifactId>mapstruct-processor</artifactId>
  18. <version>${mapstruct.version}</version>
  19. </path>
  20. </annotationProcessorPaths>
  21. </configuration>
  22. </plugin>

然后我们就很容易将一个Java Bean转化为另一个Java Bean。下面这段代码将UserInfo转换为UserInfoVO而且自动为UserInfoVO.addTime赋值为当前时间,同时这个工具也自动注入了Spring IoC,而这一切都发生在编译期。

编译前:

  1. /**
  2. * @author felord.cn
  3. * @since 16:09
  4. **/
  5. @Mapper(componentModel = "spring", imports = {LocalDateTime.class})
  6. public interface BeanMapping {
  7. @Mapping(target = "addTime", expression = "java(LocalDateTime.now())")
  8. UserInfoVO toUserInfoVo(UserInfo userInfo);
  9. }

编译后:

  1. package cn.felord.kono.beanmapping;
  2. import cn.felord.kono.entity.UserInfo;
  3. import cn.felord.kono.entity.UserInfoVO;
  4. import java.time.LocalDateTime;
  5. import javax.annotation.Generated;
  6. import org.springframework.stereotype.Component;
  7. @Generated(
  8. value = "org.mapstruct.ap.MappingProcessor",
  9. date = "2020-07-30T23:11:24+0800",
  10. comments = "version: 1.3.0.Final, compiler: javac, environment: Java 1.8.0_252 (AdoptOpenJDK)"
  11. )
  12. @Component
  13. public class BeanMappingImpl implements BeanMapping {
  14. @Override
  15. public UserInfoVO toUserInfoVo(UserInfo userInfo) {
  16. if ( userInfo == null ) {
  17. return null;
  18. }
  19. UserInfoVO userInfoVO = new UserInfoVO();
  20. userInfoVO.setName( userInfo.getName() );
  21. userInfoVO.setAge( userInfo.getAge() );
  22. userInfoVO.setAddTime( LocalDateTime.now() );
  23. return userInfoVO;
  24. }
  25. }

其实mapStruct也就是帮我们写了GetterSetter,但是不要使用其比较复杂的转换,会增加学习成本和可维护的难度。

4. 单元测试

将以上功能集成进去后分别做一个单元测试,全部通过。

  1. @Autowired
  2. MockMvc mockMvc;
  3. @Autowired
  4. BeanMapping beanMapping;
  5. /**
  6. * 测试全局异常处理.
  7. *
  8. * @throws Exception the exception
  9. * @see UserController#getUserInfo()
  10. */
  11. @Test
  12. void testGlobalExceptionHandler() throws Exception {
  13. String rtnJsonStr = "{\n" +
  14. " \"code\": 700,\n" +
  15. " \"data\": null,\n" +
  16. " \"msg\": \"test global exception handler\",\n" +
  17. " \"identifier\": \"-9999\"\n" +
  18. "}";
  19. mockMvc.perform(MockMvcRequestBuilders.get("/user/get"))
  20. .andExpect(MockMvcResultMatchers.content()
  21. .json(rtnJsonStr))
  22. .andDo(MockMvcResultHandlers.print());
  23. }
  24. /**
  25. * 测试统一返回体.
  26. *
  27. * @throws Exception the exception
  28. * @see UserController#getUserVO()
  29. */
  30. @Test
  31. void testUnifiedReturnStruct() throws Exception {
  32. // "{\"code\":200,\"data\":{\"name\":\"felord.cn\",\"age\":18,\"addTime\":\"2020-07-30T13:08:53.201\"},\"msg\":\"\",\"identifier\":\"\"}";
  33. mockMvc.perform(MockMvcRequestBuilders.get("/user/vo"))
  34. .andExpect(MockMvcResultMatchers.jsonPath("code", Is.is(200)))
  35. .andExpect(MockMvcResultMatchers.jsonPath("data.name", Is.is("felord.cn")))
  36. .andExpect(MockMvcResultMatchers.jsonPath("data.age", Is.is(18)))
  37. .andExpect(MockMvcResultMatchers.jsonPath("data.addTime", Is.is(notNullValue())))
  38. .andDo(MockMvcResultHandlers.print());
  39. }
  40. /**
  41. * 测试 mapStruct类型转换.
  42. *
  43. * @see BeanMapping
  44. */
  45. @Test
  46. void testMapStruct() {
  47. UserInfo userInfo = new UserInfo();
  48. userInfo.setName("felord.cn");
  49. userInfo.setAge(18);
  50. UserInfoVO userInfoVO = beanMapping.toUserInfoVo(userInfo);
  51. Assertions.assertEquals(userInfoVO.getName(), userInfo.getName());
  52. Assertions.assertNotNull(userInfoVO.getAddTime());
  53. }

5. 总结

自制脚手架初步具有了统一返回体统一异常处理快速类型转换,其实参数校验也已经支持了。后续就该整合数据库了,常用的数据库访问技术主要为MybatisSpring Data JPAJOOQ等,不知道你更喜欢哪一款?欢迎留言讨论。

关注公众号:Felordcn 获取更多资讯

个人博客:https://felord.cn

从零搭建Spring Boot脚手架(2):增加通用的功能的更多相关文章

  1. 从零搭建Spring Boot脚手架(1):开篇以及技术选型

    1. 前言 目前Spring Boot已经成为主流的Java Web开发框架,熟练掌握Spring Boot并能够根据业务来定制Spring Boot成为一个Java开发者的必备技巧,但是总是零零碎碎 ...

  2. 从零搭建Spring Boot脚手架(3):集成mybatis

    1. 前言 今天继续搭建我们的kono Spring Boot脚手架,上一文集成了一些基础的功能,比如统一返回体.统一异常处理.快速类型转换.参数校验等常用必备功能,并编写了一些单元测试进行验证,今天 ...

  3. 从零搭建Spring Boot脚手架(4):手写Mybatis通用Mapper

    1. 前言 今天继续搭建我们的kono Spring Boot脚手架,上一文把国内最流行的ORM框架Mybatis也集成了进去.但是很多时候我们希望有一些开箱即用的通用Mapper来简化我们的开发.我 ...

  4. 从零搭建Spring Boot脚手架(7):整合OSS作为文件服务器

    1. 前言 文件服务器是一个应用必要的组件之一.最早我搞过FTP,然后又用过FastDFS,接私活的时候我用MongoDB也凑合凑合.现如今时代不同了,开始流行起了OSS. Gitee: https: ...

  5. 从零搭建Spring Boot脚手架(7):Elasticsearch应该独立服务

    1. Spring Data Elasticsearch Spring Data Elasticsearch是Spring Data项目的子项目,提供了Elasticsearch与Spring的集成. ...

  6. 从零搭建Spring Boot脚手架(5):整合 Mybatis Plus

    1. 前言 在上一文中我根据Mybatis中Mapper的生命周期手动实现了一个简单的通用Mapper功能,但是遗憾的是它缺乏实际生产的检验.因此我选择更加成熟的一个Mybatis开发增强包.它就是已 ...

  7. 从零搭建Spring Boot脚手架(6):整合Redis作为缓存

    1. 前言 上一文我们整合了Mybatis Plus,今天我们会把缓存也集成进来.缓存是一个系统应用必备的一种功能,除了在减轻数据库的压力之外.还在存储一些短时效的数据场景中发挥着重大作用,比如存储用 ...

  8. Spring Boot实战一:搭建Spring Boot开发环境

    一开始接触Spring Boot就感到它非常强大,也非常简单实用,遂想将其记录下来. 搭建Spring Boot工程非常简单,到:http://start.spring.io/ 下载Spring Bo ...

  9. 使用IDEA搭建Spring Boot入门项目

    简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置 ...

随机推荐

  1. Java实现 LeetCode第30场双周赛 (题号5177,5445,5446,5447)

    这套题不算难,但是因为是昨天晚上太晚了,好久没有大晚上写过代码了,有点不适应,今天上午一看还是挺简单的 5177. 转变日期格式   给你一个字符串 date ,它的格式为 Day Month Yea ...

  2. 从零开始学Electron笔记(五)

    在之前的文章我们介绍了一下Electron的右键菜单的制作,接下来我们继续说一下Electron如何通过链接打开浏览器和嵌入网页. 现在有这样一个需求,我们要在我们的软件中加一个链接,然后点击该链接打 ...

  3. hls&flv直播请求过程

    hls&flv直播请求过程 直播类产品层出不穷,从各方面塑造了我们的生活方式.直播产品中,延时是决定用户体验的关键因素,它也将间接决定直播产品的成败.这其间,对延时影响较大的就是直播架构中选择 ...

  4. elementUI form表单验证不通过的三个原因

    <el-form :model="form" :rules="rules"> <el-form-item prop="input&q ...

  5. 带你理解Lock锁原理

    同样是锁,先说说synchronized和lock的区别: synchronized是java关键字,是用c++实现的:而lock是用java类,用java可以实现 synchronized可以锁住代 ...

  6. Eclipse普通java Project文件路径问题

    Eclipse普通java Project文件路径问题 项目的结构如图 读取src里某个包下的文件,代码如下 BufferedReader br=new BufferedReader(new File ...

  7. jmeter配置原件之使用CSV Data Set Config参数化

    测试过程中经常需要对发送的请求进行参数化,jmeter提供的CSV Data Set Config 配置元件可以很好的对请求数据进行参数化,下面介绍使用CSV Data Set Config参数化 1 ...

  8. web自动化 -- 消息提示框处理 (alert、confirm、prompt)

    一.前提知识 1.警告消息框(alert) 警告消息框提供了一个"确定"按钮让用户关闭该消息框,并且该消息框是模式对话框,也就是说用户必须先关闭该消息框然后才能继续进行操作. 2. ...

  9. 2Ants(独立,一个个判,弹性碰撞,想象)

    AntsDescriptionAn army of ants walk on a horizontal pole of length l cm, each with a constant speed ...

  10. File类的基本概念与递归

    一.File类 1.概念 File类:是文件和目录路径名的抽象表示形式. 即,Java中把文件或者目录(文件夹)都封装成File对象.也就是说如果我们要去操作硬盘上的文件,或者文件夹只要找到File这 ...