[Spring cloud realizes advertising system step by step] 4. General code module design

Posted by FiveFlat on Sat, 27 Jul 2019 14:16:22 +0200

A large system must be essential for code reuse. It can solve the following problems:

  1. Unified Response Processing (Unified Response Object Packaging for Outside)

    graph LR
    HTTP-->|HttpRequest|RestController
    RestController-->|HttpResponse -> JSON|HTTP  
  2. Unified exception handling (business exceptions can be collected and handled uniformly)

    graph TB
        subgraph Processing process
            AdCommonException -->|RestControllerAdvice| HttpRequest
        end
        subgraph HttpResponse
            RestControllerAdvice -->|Unified Return| JSON
    end
    
  3. Universal Code Definition and Configuration Definition (Universal Configuration Information is placed in Unified Code Management for easy maintenance and updating)

Create project mscx-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>Public Logic and Help class</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
          <!-- fastjson Is Alibaba's Open Source JSON Parsing libraries, which can parse JSON A formatted string that supports the Java Bean Serialization JSON String, or from JSON String deserialization to 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 Compiler plug-in-->
    <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 Advice

    • Before (before method execution) org.apringframework.aop.MethodBeforeAdvice
    • AfterReturning (after method returns) org. spring framework. aop. AfterReturning Advice
    • After-throwing (after an exception is thrown) surrounded by org.spring framework.aop.ThrowsAdviceArround, that is, before and after the method, org.aopaliance.intercept.MethodInterceptor
      Introduction, not commonly used org. spring framework. aop. IntroductionInterceptor

    Specific reference: Detailed advice,advisor

  • annotation
  • config
  • exception
  • utils
  • export
Universal Response Coding
  1. Create a generic return object
/**
* @Data It's a combination of subordinate annotations.
* 
* @see Getter
* @see Setter
* @see RequiredArgsConstructor
* @see ToString
* @see EqualsAndHashCode
* @see lombok.Value 
*/
@Data
@NoArgsConstructor //non-parameter constructor
@AllArgsConstructor //Full-parameter constructor
public class CommonResponse<T> implements Serializable {
   private Integer code = 0;
   private String message = "success";
   /**
    * Specific data object information
    */
   private T data;

   public CommonResponse(Integer code, String message) {
       this.code = code;
       this.message = message;
   }

   public CommonResponse(T data) {
       this.data = data;
   }
}
  1. Implement a unified interception of responses in the advice package, com.sxzhongf.ad.common.advice.CommonResponseData Advice, for reference ResponseBodyAdvice RestController Advice can view the source code org. spring framework. web. servlet. mvc. method. annotation. ResponseBodyAdvice
   @RestControllerAdvice
   public class CommonResponseDataAdvice implements ResponseBodyAdvice<Object> {
   
       /**
        * Determine whether the response needs to be processed
        *
        * @return false -> No treatment, true - > treatment
        */
       @Override
       public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> converterType) {
   //
   //        // The method of getting the controller that is currently processing the request
   //        String methodName = methodParameter.getMethod().getName().toLowerCase();
   //        // Methods that do not intercept/do not need to process return values
   //        String method = login; //e.g. login
   //        // No interception
   //        return !method.equals(methodName);
   
           // If @IgnoreResponseAdvice is marked on the class, it is not intercepted
           if (methodParameter.getDeclaringClass().isAnnotationPresent(IgnoreResponseAdvice.class)) {
               return false;
           }
   
           // If @IgnoreResponseAdvice is marked on the method, it is not intercepted
           if (methodParameter.getMethod().isAnnotationPresent(IgnoreResponseAdvice.class)) {
               return false;
           }
   
           //Processing the response and executing the beforeBodyWrite method
           return true;
       }
   
       /**
        * Aim to intercept Common Response
        *
        * @param body The original Controller needs to return data
        */
       @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 a comment com.sxzhongf.ad.common.annotation.IgnoreResponseAdvice under the annotation package to indicate whether the column needs to support the unified return interception above.

   /**
    * IgnoreResponseAdvice for Marking Needs to Ignore Interception Action
    *
    * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang</a>
    */
   //ElementType.TYPE indicates that the annotation can be used for class
   //ElementType.METHOD representations can be used for methods
   @Target({ElementType.TYPE, ElementType.METHOD})
   @Retention(RetentionPolicy.RUNTIME)
   public @interface IgnoreResponseAdvice {
   }
General exception handling

Exception handling is unified, so RestController Advice is also used, and Spring Exception Handler is needed for exception handling.

  1. Creating a Unified Exception Interception Class
/**
 * GlobalExceptionAdvice for Global Unified Anomaly Interception
 *
 * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang</a>
 * @see RestControllerAdvice
 * @see ExceptionHandler
 */
@RestControllerAdvice
public class GlobalExceptionAdvice {

    /**
     * Unified Processing of {@link AdException}
     * {@link ExceptionHandler}  Intercept specified exceptions
     * Optimizable:
     * Define a variety of exceptions and implement corresponding exception handling.
     * For example:
     * <ul>
     * <li>
     * Popularize unit operation exception, throw AdUnitException
     * </li>
     * <li>
     * Binlog Resolve exceptions and throw BinlogException
     * </li>
     * </ul>
     * Intercepting Spring Exception with {@link Exception Handler} annotation
     */
    @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 generic exception classes
/**
 * AdException for Unified exception handling class
 *
 * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang</a>
 */
public class AdException extends Exception {
    public AdException(String message) {
        super(message);
    }
}
General configuration information

Through HTTP message converter HttpMessageConverter, object conversion is realized, Java Object - > HTTP data stream

  1. Adding WebConfiguration, we customize and modify the configuration information of Spring MVC by implementing org. spring framework. web. servlet. config. annotation. WebMvcConfigurer.
/**
 * WebConfiguration for Customize Spring configuration and behavior
 *
 * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang</a>
 * @see WebMvcConfigurer
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
    /**
     * Matching Routing Request Rules
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {

    }
    /**
     * Register custom Formatter and Convert
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {

    }
    /**
     * Adding a Static Resource Processor
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

    }
    /**
     * Add custom view controller
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

    }
    /**
     * Add a custom method parameter processor
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {

    }
    /**
     * Configure Message Converter
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //Clear all converters
        converters.clear();
          // Java Obj -> Json Obj (http header: application/json)
        converters.add(new MappingJackson2HttpMessageConverter());
    }
}

Be a good person.

Blog Garden | segmentfault | spring4all | csdn | Nuggets | OSChina | Brief book | Headlines | Know about | 51CTO

Topics: Java Spring JSON Maven Apache