一個大的系統,在代碼的複用確定是必不可少的,它能解決:html
統一的響應處理(能夠對外提供統一的響應對象包裝)java
graph LR HTTP-->|HttpRequest|RestController RestController-->|HttpResponse -> JSON|HTTP
統一的異常處理(能夠將業務異常統一收集處理)web
graph TB subgraph 處理過程 AdCommonException -->|RestControllerAdvice| HttpRequest end subgraph HttpResponse RestControllerAdvice -->|統一返回| JSON end
建立項目
mscx-ad-common
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>mscx-ad</artifactId> <groupId>com.sxzhongf</groupId> <version>1.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <modelVersion>4.0.0</modelVersion> <packaging>jar</packaging> <groupId>com.sxzhongf</groupId> <artifactId>mscx-ad-common</artifactId> <version>1.0-SNAPSHOT</version> <name>Common-Service</name> <description>公共邏輯 and 幫助類</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- fastjson是阿里巴巴的開源JSON解析庫,它能夠解析JSON格式的字符串,支持將Java Bean序列化爲JSON字符串,也能夠從JSON字符串反序列化到JavaBean --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.58</version> </dependency> <!-- --> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> </dependencies> <!--maven編譯插件--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
advice (bean 加強package )spring
Spring支持五種類型的加強或通知(Advice)apache
- Before(方法執行前)
org.apringframework.aop.MethodBeforeAdvice
- AfterReturning(方法返回後)
org.springframework.aop.AfterReturningAdvice
- After-throwing(異常拋出後)
org.springframework.aop.ThrowsAdviceArround
環繞,即方法先後org.aopaliance.intercept.MethodInterceptor
引介,不經常使用org.springframework.aop.IntroductionInterceptor
具體可參考:細說advice,advisorjson
/** * @Data是下屬註解的組合註解 * * @see Getter * @see Setter * @see RequiredArgsConstructor * @see ToString * @see EqualsAndHashCode * @see lombok.Value */ @Data @NoArgsConstructor //無參構造函數 @AllArgsConstructor //全參構造函數 public class CommonResponse<T> implements Serializable { private Integer code = 0; private String message = "success"; /** * 具體的數據對象信息 */ private T data; public CommonResponse(Integer code, String message) { this.code = code; this.message = message; } public CommonResponse(T data) { this.data = data; } }
com.sxzhongf.ad.common.advice.CommonResponseDataAdvice
,參考 ResponseBodyAdvice, RestControllerAdvice
可查看源碼org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice
@RestControllerAdvice public class CommonResponseDataAdvice implements ResponseBodyAdvice<Object> { /** * 判斷是否須要對響應進行處理 * * @return false -> 不處理,true -> 處理 */ @Override public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> converterType) { // // //獲取當前處理請求的controller的方法 // String methodName = methodParameter.getMethod().getName().toLowerCase(); // // 不攔截/不須要處理返回值 的方法 // String method = "login"; //如登陸 // //不攔截 // return !method.equals(methodName); // 若是類上標記了@IgnoreResponseAdvice,則不攔截 if (methodParameter.getDeclaringClass().isAnnotationPresent(IgnoreResponseAdvice.class)) { return false; } // 若是方法上標記了@IgnoreResponseAdvice,則不攔截 if (methodParameter.getMethod().isAnnotationPresent(IgnoreResponseAdvice.class)) { return false; } //對響應進行處理,執行beforeBodyWrite方法 return true; } /** * 目的 攔截CommonResponse * * @param body 原始的Controller須要返回的數據 */ @Override public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { CommonResponse<Object> commonResponse = new CommonResponse<>(); if (null == body) { return commonResponse; } else if (body instanceof CommonResponse) { commonResponse = (CommonResponse<Object>) body; } else { commonResponse.setData(body); } return commonResponse; } }
咱們在annotation包下面添加一個註解com.sxzhongf.ad.common.annotation.IgnoreResponseAdvice
,用它來標柱是否須要支持上面的統一返回攔截。segmentfault
/** * IgnoreResponseAdvice for 標示須要忽略攔截動做 * * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang</a> */ //ElementType.TYPE 表示該註解可用於class //ElementType.METHOD 表示可用於方法 @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface IgnoreResponseAdvice { }
異常處理也是統一的,那麼一樣就要使用到RestControllerAdvice
,同時,須要使用的Spring 的ExceptionHandler
進行異常處理
/** * GlobalExceptionAdvice for 全局統一異常攔截 * * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang</a> * @see RestControllerAdvice * @see ExceptionHandler */ @RestControllerAdvice public class GlobalExceptionAdvice { /** * 對 {@link AdException} 進行統一處理 * {@link ExceptionHandler} 對指定的異常進行攔截 * 可優化: * 定義多種類異常,實現對應的異常處理, * 例如: * <ul> * <li> * 推廣單元操做異常,拋出 AdUnitException * </li> * <li> * Binlog 解析異常,拋出 BinlogException * </li> * </ul> * 攔截Spring Exception 使用 {@link ExceptionHandler}註解 */ @ExceptionHandler(value = AdException.class) public CommonResponse<String> handlerAdException(HttpServletRequest request, AdException ex) { CommonResponse<String> response = new CommonResponse<>(-1, "business error"); response.setData(ex.getMessage()); return response; } }
/** * AdException for 統一異常處理類 * * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang</a> */ public class AdException extends Exception { public AdException(String message) { super(message); } }
經過HTTP消息轉換器HttpMessageConverter
,實現對象轉換,Java Object
-> HTTP 數據流
api
WebConfiguration
,咱們經過實現org.springframework.web.servlet.config.annotation.WebMvcConfigurer
來定製和修改Spring MVC的配置信息。/** * WebConfiguration for 對Spring的配置和行爲進行定製修改 * * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang</a> * @see WebMvcConfigurer */ @Configuration public class WebConfiguration implements WebMvcConfigurer { /** * 匹配路由請求規則 */ @Override public void configurePathMatch(PathMatchConfigurer configurer) { } /** * 註冊自定義的Formatter 和 Convert */ @Override public void addFormatters(FormatterRegistry registry) { } /** * 添加靜態資源處理器 */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { } /** * 添加自定義視圖控制器 */ @Override public void addViewControllers(ViewControllerRegistry registry) { } /** * 添加自定義方法參數處理器 */ @Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { } /** * 配置消息轉換器 */ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { //清空全部轉換器 converters.clear(); // Java Obj -> Json Obj (http header: application/json) converters.add(new MappingJackson2HttpMessageConverter()); } }
博客園 | segmentfault | spring4all | csdn | 掘金 | OSChina | 簡書 | 頭條 | 知乎 | 51CTOmvc