Custom annotation interface parameters to optimize check

  When actual development, many of the service interface parameters as complex as a multi-stage will be mixed with various JSON or nested array.

  Assembled logic this time if we will write cluttered interface parameters in the Controller layer, the readability of the code will be very poor, who need a follow-up to take over the front interface document assembly logic than to deduce the parameters of a parameter of the parameter.

  In this case, the interface call parameters DTO objects encapsulate data transmission, with the custom annotations can greatly improve the readability.

  We customize a comment to identify the parameters of our dto:

@Retention (RetentionPolicy.RUNTIME) 
@Target (value = ElementType.FIELD)
 public @ interface ParamAnnotation {
     // if necessary parameters 
    public  Boolean isRequired () default  to false ; 

    // parameter name 
    public String name (); 
}

  Notes contains two properties, the parameter name and parameter if necessary.

  In this way, our code consists of:

 

   Changes to the:

 

  Significantly improved readability, while we can group related assignment logic is encapsulated in the constructor's dto.

  Before each invocation interfaces, whether we can complete by reflection in Traverse Checking dto must pass parameters:

 / ** 
     * @author Nxy 
     * @date 2020/3/24 19:49 
     * @Param obj: a need to verify the parameter object 
     * @return 
     * @exception 
     * @Description determine whether the parameters are required to fill in 
     * / 
    public  Boolean isComplete (T obj) throws IllegalAccessException, a NoSuchFieldException { 
        class objclass = obj.getClass (); 
        Field, Fields [] = objClass.getDeclaredFields ();
         for (Field, Field: Fields) {
             IF (. field.isAnnotationPresent (ParamAnnotation class )) { 
                field.setAccessible ( to true );
                ParamAnnotation ParamAnnotation = field.getAnnotation (ParamAnnotation. Class );
                 // if required 
                IF (paramAnnotation.isRequired ()) { 
                    Object value = Field.get (obj);
                     IF ( null == value) { 
                        Object ParaName = paramAnnotation .name ();
                         // missing items writeback 
                        Field, deletion = objClass.getDeclaredField ( "deletion" ); 
                        deletion.setAccessible ( to true );
                        deletion.set(obj, paraName);
                        return false;
                    }
                }
            }
        }
        return true;
    }

  So that our outer layer logic by the dozens of lines of code becomes:

 

   Vo dto obtained by the parameter, the integrity check parameter. Let logic controller only to call attention to the rest piecemeal conversion and check modular.

  While reflecting lower operating efficiency, but not particularly high on the performance requirements of the scene, with these costs in exchange for the code readability is worth it.

Guess you like

Origin www.cnblogs.com/niuyourou/p/12561939.html