zoukankan      html  css  js  c++  java
  • ResponseUtil反射制造唯一结果

    调用:通过反射调用同一个类型的返回值

    return fillResponse(response,Constants.SUCCESS,"获取数据成功","taskList",taskList);

    (1)

    /**
         * 回复结果
         * @param obj
         * @param code
         * @param description
         * @param paramName
         * @param paramValue
         * @return
         */
        private String fillResponse(final Object obj ,final String code , final String description,final String paramName,final Object paramValue ){
            return ResponseUtil.fillResponse(obj, code, description, paramName, paramValue);
        }
        private String fillResponseBase(final Object obj ,final String code , final String description){
            return ResponseUtil.fillResponseBase(obj, code, description);
        }

    (2)

    package com.venustech.scanner.webservice.vulntask.util;
    import java.lang.reflect.Method;
    
    import com.venustech.vwf.utils.json.JsonUtils;
    /**
     * 
     * 类说明.优化使用
     * <pre>
     * 2015年12月11日 songjian    
     * </pre>
     */
    public class ResponseUtil {
        /**
         * 针对只有code和description的情况 +
         * @param object
         * @param code
         * @param description
         * @param paramName
         * @param paramVal
         * @return
         */
        public static <T> String fillResponse(final T object,final String code , final String description ,final String paramName , final Object paramVal) {  
             if (object == null) {  
                   return null;  
               }  
               Class<T> clazz = (Class<T>) object.getClass(); 
                 try {  
                     Method get_Method_code = clazz.getMethod("getCode" );  //获取getMethod方法  
                     Method set_Method_code = clazz.getMethod("setCode", get_Method_code.getReturnType());//获得属性set方法  
                     set_Method_code.invoke(object, code);
                     
                     Method get_Method_des= clazz.getMethod("getDescription" );  //获取getMethod方法  
                     Method set_Method_des = clazz.getMethod("setDescription", get_Method_des.getReturnType());//获得属性set方法  
                     set_Method_des.invoke(object, description);
                    if(paramName!=""){
                         Method get_Method = clazz.getMethod("get" + getMethodName(paramName));  //获取getMethod方法  
                         Method set_Method = clazz.getMethod("set" + getMethodName(paramName), get_Method.getReturnType());//获得属性set方法  
                         set_Method.invoke(object, paramVal);
                    } 
                    } catch (Exception e) {  
                    }  
               return JsonUtils.toJson(object); 
        } 
        /**
         * 针对只有code和description的情况
         * @param object
         * @param code
         * @param description
         * @return
         */
        public static <T> String fillResponseBase(final T object,final String code , final String description) {  
             if (object == null) {  
                   return null;  
               }  
               Class<T> clazz = (Class<T>) object.getClass(); 
                 try {  
                     Method get_Method_code = clazz.getMethod("getCode" );  //获取getMethod方法  
                     Method set_Method_code = clazz.getMethod("setCode", get_Method_code.getReturnType());//获得属性set方法  
                     set_Method_code.invoke(object, code);
                     
                     Method get_Method_des= clazz.getMethod("getDescription" );  //获取getMethod方法  
                     Method set_Method_des = clazz.getMethod("setDescription", get_Method_des.getReturnType());//获得属性set方法  
                     set_Method_des.invoke(object, description);
                    } catch (Exception e) {  
                    }  
               return JsonUtils.toJson(object); 
        } 
        
       private static String getMethodName(final String fildeName) {  
           byte[] items = fildeName.getBytes();  
           items[0] = (byte) ((char) items[0] - 'a' + 'A');  
           return new String(items);  
       } 
    }
  • 相关阅读:
    二叉树——Java实现
    Java实现单链表的增删查改及逆置打印
    常见排序——Java实现
    [导入]Interesting Finds: 2007.12.10
    [导入]Interesting Finds: 2007.12.09
    [导入]Interesting Finds: 2007.12.12
    [导入]Interesting Finds: 2007.12.06
    [导入]Interesting Finds: 2007.12.07
    [导入]Interesting Finds: 2007.12.04
    [导入]Interesting Finds: 2007.12.05
  • 原文地址:https://www.cnblogs.com/sj521/p/5130063.html
Copyright © 2011-2022 走看看