今天开始搭建咱们的kono Spring Boot脚手架,首先会集成Spring MVC并进行定制化以知足平常开发的须要,咱们先作一些刚性的需求定制,后续再补充细节。若是你看了本文有什么问题能够留言讨论。多多持续关注,共同窗习,共同进步。html
Gitee: https://gitee.com/felord/konoGitHub: https://github.com/NotFound40...前端
在开发中统一返回数据很是重要。方便前端统一处理。一般设计为如下结构:java
{ "code": 200, "data": { "name": "felord.cn", "age": 18 }, "msg": "", "identifier": "" }
根据上面的一些定义,声明了一个统一返回体对象RestBody<T>
并声明了一些静态方法来方便定义。git
package cn.felord.kono.advice; import lombok.Data; import java.io.Serializable; /** * @author felord.cn * @since 22:32 2019-04-02 */ @Data public class RestBody<T> implements Rest<T>, Serializable { private static final long serialVersionUID = -7616216747521482608L; private int code = 200; private T data; private String msg = ""; private String identifier = ""; public static Rest<?> ok() { return new RestBody<>(); } public static Rest<?> ok(String msg) { Rest<?> restBody = new RestBody<>(); restBody.setMsg(msg); return restBody; } public static <T> Rest<T> okData(T data) { Rest<T> restBody = new RestBody<>(); restBody.setData(data); return restBody; } public static <T> Rest<T> okData(T data, String msg) { Rest<T> restBody = new RestBody<>(); restBody.setData(data); restBody.setMsg(msg); return restBody; } public static <T> Rest<T> build(int code, T data, String msg, String identifier) { Rest<T> restBody = new RestBody<>(); restBody.setCode(code); restBody.setData(data); restBody.setMsg(msg); restBody.setIdentifier(identifier); return restBody; } public static Rest<?> failure(String msg, String identifier) { Rest<?> restBody = new RestBody<>(); restBody.setMsg(msg); restBody.setIdentifier(identifier); return restBody; } public static Rest<?> failure(int httpStatus, String msg ) { Rest<?> restBody = new RestBody< >(); restBody.setCode(httpStatus); restBody.setMsg(msg); restBody.setIdentifier("-9999"); return restBody; } public static <T> Rest<T> failureData(T data, String msg, String identifier) { Rest<T> restBody = new RestBody<>(); restBody.setIdentifier(identifier); restBody.setData(data); restBody.setMsg(msg); return restBody; } @Override public String toString() { return "{" + "code:" + code + ", data:" + data + ", msg:" + msg + ", identifier:" + identifier + '}'; } }
可是每次都要显式声明返回体也不是很优雅的办法,因此咱们但愿无感知的来实现这个功能。Spring Framework正好提供此功能,咱们借助于@RestControllerAdvice
和ResponseBodyAdvice<T>
来对项目的每个@RestController
标记的控制类的响应体进行后置切面通知处理。github
/** * 统一返回体包装器 * * @author felord.cn * @since 14:58 **/ @RestControllerAdvice public class RestBodyAdvice implements ResponseBodyAdvice<Object> { @Override public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) { return true; } @Override public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) { // 若是为空 返回一个不带数据的空返回体 if (o == null) { return RestBody.ok(); } // 若是 RestBody 的 父类 是 返回值的父类型 直接返回 // 方便咱们能够在接口方法中直接返回RestBody if (Rest.class.isAssignableFrom(o.getClass())) { return o; } // 进行统一的返回体封装 return RestBody.okData(o); } }
当咱们接口返回一个实体类时会自动封装到统一返回体RestBody<T>
中。spring
既然有ResponseBodyAdvice
,就有一个RequestBodyAdvice
,它彷佛是来进行前置处理的,之后可能有一些用途。
统一异常也是@RestControllerAdvice
能实现的,可参考以前的Hibernate Validator校验参数全攻略。这里初步集成了校验异常的处理,后续会添加其余异常。数据库
/** * 统一异常处理 * * @author felord.cn * @since 13 :31 2019-04-11 */ @Slf4j @RestControllerAdvice public class ApiExceptionHandleAdvice { @ExceptionHandler(BindException.class) public Rest<?> handle(HttpServletRequest request, BindException e) { logger(request, e); List<ObjectError> allErrors = e.getAllErrors(); ObjectError objectError = allErrors.get(0); return RestBody.failure(700, objectError.getDefaultMessage()); } @ExceptionHandler(MethodArgumentNotValidException.class) public Rest<?> handle(HttpServletRequest request, MethodArgumentNotValidException e) { logger(request, e); List<ObjectError> allErrors = e.getBindingResult().getAllErrors(); ObjectError objectError = allErrors.get(0); return RestBody.failure(700, objectError.getDefaultMessage()); } @ExceptionHandler(ConstraintViolationException.class) public Rest<?> handle(HttpServletRequest request, ConstraintViolationException e) { logger(request, e); Optional<ConstraintViolation<?>> first = e.getConstraintViolations().stream().findFirst(); String message = first.isPresent() ? first.get().getMessage() : ""; return RestBody.failure(700, message); } @ExceptionHandler(Exception.class) public Rest<?> handle(HttpServletRequest request, Exception e) { logger(request, e); return RestBody.failure(700, e.getMessage()); } private void logger(HttpServletRequest request, Exception e) { String contentType = request.getHeader("Content-Type"); log.error("统一异常处理 uri: {} content-type: {} exception: {}", request.getRequestURI(), contentType, e.toString()); } }
简化Java Bean之间转换也是一个必要的功能。 这里选择mapStruct,类型安全并且容易使用,比那些BeanUtil
要好用的多。可是从我使用的经验上来看,不要使用mapStruct提供的复杂功能只作简单映射。详细可参考文章Spring Boot 2 实战:集成 MapStruct 类型转换。express
集成进来很是简单,因为它只在编译期生效因此引用时的scope
最好设置为compile
,咱们在kono-dependencies中加入其依赖管理:apache
<dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> <version>${mapstruct.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${mapstruct.version}</version> <scope>compile</scope> </dependency>
在kono-app
中直接引用上面两个依赖,可是这样还不行,和lombok一块儿使用编译容易出现SPI错误。咱们还须要集成相关的Maven插件到kono-app编译的生命周期中去。参考以下:json
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <showWarnings>true</showWarnings> <annotationProcessorPaths> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </path> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${mapstruct.version}</version> </path> </annotationProcessorPaths> </configuration> </plugin>
而后咱们就很容易将一个Java Bean转化为另外一个Java Bean。下面这段代码将UserInfo
转换为UserInfoVO
并且自动为UserInfoVO.addTime
赋值为当前时间,同时这个工具也自动注入了Spring IoC,而这一切都发生在编译期。
编译前:
/** * @author felord.cn * @since 16:09 **/ @Mapper(componentModel = "spring", imports = {LocalDateTime.class}) public interface BeanMapping { @Mapping(target = "addTime", expression = "java(LocalDateTime.now())") UserInfoVO toUserInfoVo(UserInfo userInfo); }
编译后:
package cn.felord.kono.beanmapping; import cn.felord.kono.entity.UserInfo; import cn.felord.kono.entity.UserInfoVO; import java.time.LocalDateTime; import javax.annotation.Generated; import org.springframework.stereotype.Component; @Generated( value = "org.mapstruct.ap.MappingProcessor", date = "2020-07-30T23:11:24+0800", comments = "version: 1.3.0.Final, compiler: javac, environment: Java 1.8.0_252 (AdoptOpenJDK)" ) @Component public class BeanMappingImpl implements BeanMapping { @Override public UserInfoVO toUserInfoVo(UserInfo userInfo) { if ( userInfo == null ) { return null; } UserInfoVO userInfoVO = new UserInfoVO(); userInfoVO.setName( userInfo.getName() ); userInfoVO.setAge( userInfo.getAge() ); userInfoVO.setAddTime( LocalDateTime.now() ); return userInfoVO; } }
其实mapStruct也就是帮咱们写了Getter和Setter,可是不要使用其比较复杂的转换,会增长学习成本和可维护的难度。
将以上功能集成进去后分别作一个单元测试,所有经过。
@Autowired MockMvc mockMvc; @Autowired BeanMapping beanMapping; /** * 测试全局异常处理. * * @throws Exception the exception * @see UserController#getUserInfo() */ @Test void testGlobalExceptionHandler() throws Exception { String rtnJsonStr = "{\n" + " \"code\": 700,\n" + " \"data\": null,\n" + " \"msg\": \"test global exception handler\",\n" + " \"identifier\": \"-9999\"\n" + "}"; mockMvc.perform(MockMvcRequestBuilders.get("/user/get")) .andExpect(MockMvcResultMatchers.content() .json(rtnJsonStr)) .andDo(MockMvcResultHandlers.print()); } /** * 测试统一返回体. * * @throws Exception the exception * @see UserController#getUserVO() */ @Test void testUnifiedReturnStruct() throws Exception { // "{\"code\":200,\"data\":{\"name\":\"felord.cn\",\"age\":18,\"addTime\":\"2020-07-30T13:08:53.201\"},\"msg\":\"\",\"identifier\":\"\"}"; mockMvc.perform(MockMvcRequestBuilders.get("/user/vo")) .andExpect(MockMvcResultMatchers.jsonPath("code", Is.is(200))) .andExpect(MockMvcResultMatchers.jsonPath("data.name", Is.is("felord.cn"))) .andExpect(MockMvcResultMatchers.jsonPath("data.age", Is.is(18))) .andExpect(MockMvcResultMatchers.jsonPath("data.addTime", Is.is(notNullValue()))) .andDo(MockMvcResultHandlers.print()); } /** * 测试 mapStruct类型转换. * * @see BeanMapping */ @Test void testMapStruct() { UserInfo userInfo = new UserInfo(); userInfo.setName("felord.cn"); userInfo.setAge(18); UserInfoVO userInfoVO = beanMapping.toUserInfoVo(userInfo); Assertions.assertEquals(userInfoVO.getName(), userInfo.getName()); Assertions.assertNotNull(userInfoVO.getAddTime()); }
自制脚手架初步具备了统一返回体、统一异常处理、快速类型转换,其实参数校验也已经支持了。后续就该整合数据库了,经常使用的数据库访问技术主要为Mybatis、Spring Data JPA、JOOQ等,不知道你更喜欢哪一款?欢迎留言讨论。
关注公众号:Felordcn 获取更多资讯