1. 前言
明天开始搭建咱们的kono Spring Boot脚手架,首先会集成Spring MVC并进行定制化以满足日常开发的须要,咱们先做一些刚性的需要定制,后续再补充细节。如果你看了本文有什么问题能够留言探讨。多多继续关注,独特学习,共同进步。
Gitee: https://gitee.com/felord/konoGitHub: https://github.com/NotFound40...
2. 对立返回体
在开发中对立返回数据十分重要。不便前端对立解决。通常设计为以下构造:
{ "code": 200, "data": { "name": "felord.cn", "age": 18 }, "msg": "", "identifier": ""}
- code 业务状态码,设计时应该区别于http状态码。
- data 数据载体,用以装载返回给前端展示的数据。
- msg 提示信息,用于前端调用后返回的提示信息,例如 “新增胜利”、“删除失败”。
- identifier 预留的标识位,作为一些业务的解决标识。
依据下面的一些定义,申明了一个对立返回体对象RestBody<T>
并申明了一些静态方法来不便定义。
package cn.felord.kono.advice;import lombok.Data;import java.io.Serializable;/** * @author felord.cn * @since 22:32 2019-04-02 */@Datapublic 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
标记的管制类的响应体进行后置切面告诉解决。
/** * 对立返回体包装器 * * @author felord.cn * @since 14:58 **/@RestControllerAdvicepublic 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>
中。
既然有ResponseBodyAdvice
,就有一个RequestBodyAdvice
,它仿佛是来进行前置解决的,当前可能有一些用处。
2. 对立异样解决
对立异样也是@RestControllerAdvice
能实现的,可参考之前的Hibernate Validator校验参数全攻略。这里初步集成了校验异样的解决,后续会增加其余异样。
/** * 对立异样解决 * * @author felord.cn * @since 13 :31 2019-04-11 */@Slf4j@RestControllerAdvicepublic 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()); }}
3. 简化类型转换
简化Java Bean之间转换也是一个必要的性能。 这里抉择mapStruct,类型平安而且容易应用,比那些BeanUtil
要好用的多。然而从我应用的教训上来看,不要应用mapStruct提供的简单性能只做简略映射。具体可参考文章Spring Boot 2 实战:集成 MapStruct 类型转换。
集成进来非常简单,因为它只在编译期失效所以援用时的scope
最好设置为compile
,咱们在kono-dependencies中退出其依赖治理:
<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编译的生命周期中去。参考如下:
<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)")@Componentpublic 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,然而不要应用其比较复杂的转换,会减少学习老本和可保护的难度。
4. 单元测试
将以上性能集成进去后别离做一个单元测试,全副通过。
@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()); }
5. 总结
自制脚手架初步具备了对立返回体、对立异样解决、疾速类型转换,其实参数校验也曾经反对了。后续就该整合数据库了,罕用的数据库拜访技术次要为Mybatis、Spring Data JPA、JOOQ等,不晓得你更喜爱哪一款?欢送留言探讨。
关注公众号:Felordcn 获取更多资讯
集体博客:https://felord.cn