[Spring cloud realized step by step Ad System] 4. The common code module designs

A large system, code reuse is certainly essential, it can solve:

  1. Unified response processing (can begin to provide a unified response object packaging)

UTOOLS1564230299519.png

  1. Consistent exception handling (unified business exceptions can be collected and treated)

UTOOLS1564230319631.png

  1. Generic code definitions, configuration definitions (common configuration information in the unified code management, ease of maintenance and updating)

Create a projectmscx-ad-common

POM file

<?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>

Project structure

  • vo (unified response object package)

  • advice (bean enhancement package)

    Spring supports five types of reinforcing or notification (Advice)
    • Before (before the execution method) org.apringframework.aop.MethodBeforeAdvice
    • AfterReturning (method returns after) org.springframework.aop.AfterReturningAdvice
    • After-throwing (the exception was thrown) org.springframework.aop.ThrowsAdviceArroundsurrounded, i.e., after the method org.aopaliance.intercept.MethodInterceptor
      introducing, not used org.springframework.aop.IntroductionInterceptor
      specifically refer to: elaborate advice, advisor
  • annotation
  • config
  • exception
  • utils
  • export

    Universal response code
  1. Create a common return objects
/**
* @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;
   }
}
  1. To achieve a unified response to the interception advice package com.sxzhongf.ad.common.advice.CommonResponseDataAdvice, reference ResponseBodyAdvice , RestControllerAdviceview the source codeorg.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;
       }
   }

We add an annotation in annotation package below com.sxzhongf.ad.common.annotation.IgnoreResponseAdvice, use it to support the need for a unified standard column above interception return.

   /**
    * IgnoreResponseAdvice for 标示需要忽略拦截动作
    *
    * @author <a href="mailto:[email protected]">Isaac.Zhang</a>
    */
   //ElementType.TYPE 表示该注解可用于class
   //ElementType.METHOD 表示可用于方法
   @Target({ElementType.TYPE, ElementType.METHOD})
   @Retention(RetentionPolicy.RUNTIME)
   public @interface IgnoreResponseAdvice {
   }
General Exception Handling

Exception handling is uniform, then the same is necessary to use RestControllerAdvice, at the same time, Spring of the need to use ExceptionHandlerexception handling

  1. Creating a unified abnormal interception class
/**
 * GlobalExceptionAdvice for 全局统一异常拦截
 *
 * @author <a href="mailto:[email protected]">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;
    }
}
  1. Create a common exception classes
/**
 * AdException for 统一异常处理类
 *
 * @author <a href="mailto:[email protected]">Isaac.Zhang</a>
 */
public class AdException extends Exception {
    public AdException(String message) {
        super(message);
    }
}
Common configuration information

HTTP message by the converter HttpMessageConverter, the conversion to achieve the object, Java Object->HTTP 数据流

  1. New WebConfiguration, We realize org.springframework.web.servlet.config.annotation.WebMvcConfigurerto customize and modify the configuration of Spring MVC.
/**
 * WebConfiguration for 对Spring的配置和行为进行定制修改
 *
 * @author <a href="mailto:[email protected]">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());
    }
}

A good person.

Guess you like

Origin www.cnblogs.com/zhangpan1244/p/11256501.html