接口统一参数格式封装方法参数传递工具类

一、ResultInfo 实体承载类

package com.test.domi.common.system;

public class ResultInfo<T> {

    private String code;
    private String message;
    private T data;

    public ResultInfo(){
    }

    public  String getCode(){
        return this.code;
    }

    public void setCode(String code){
        this.code = code;
    }

    public  String getMessage(){
        return this.message;
    }

    public void setMessage(String message){
        this.message = message;
    }

    public  Object getData(){
        return this.data;
    }

    public void setData(T data){
        this.data = data;
    }
}

二、ResultCode 枚举类

package com.test.domi.common.system;

public enum ResultCode {

    SUCCESS("000000","成功"),
    CONNECT_ERROR("100001","网络连接失败"),
    CONNECT_TIMEOUT("100002","网络连接超时"),
    INTERNAL_SERVER_ERROR("100003","服务器内部错误"),
    QUERY_ERROR("100004","查询失败"),
    INSERT_ERROR("100005","保存数据失败"),
    UPDATE_ERROR("100006","更新数据失败"),
    DELETE_ERROR("100007","删除数据失败");

    private String code;
    private String msg;

    ResultCode(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }
}

三、ResultUtil 实例化工具类

package com.test.domi.common.system;

public class ResultUtil {

    public ResultUtil(){
    }

    public static ResultInfo getSuccessResult(Object object){
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setCode(ResultCode.SUCCESS.getCode());
        resultInfo.setMessage(ResultCode.SUCCESS.getMsg());
        resultInfo.setData(object);
        return resultInfo;
    }

    public static ResultInfo getFailResult(ResultCode resultCode){
        return getFailResult(resultCode,(Object)null);
    }

    public static ResultInfo getFailResult(String resultCode,String resultMessage,Object data){
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setCode(resultCode);
        resultInfo.setMessage(resultMessage);
        resultInfo.setData(data);
        return resultInfo;
    }

    public  static  ResultInfo getFailResult(ResultCode resultCode,Object data){
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setCode(resultCode.getCode());
        resultInfo.setMessage(resultCode.getMsg());
        resultInfo.setData(data);
        return resultInfo;
    }

}

四:业务方法之间参数传递的工具类

package com.test.domi.common.util;

import java.util.HashMap;
import java.util.Map;

/**
 * 业务方法的参数传递类
 */
public class CommonMethedParam {

    /**
     * 业务方法的参数
     */
    private Object[] args;
    /**
     * 业务方法的返回值
     */
    private Object result;
    /**
     * 自定义参数map封装
     */
    private Map<String,Object> paramMap;

    public CommonMethedParam(Object[] args){
        this.args = args;
        this.paramMap = new HashMap<>();
    }

    public CommonMethedParam(){
        this.paramMap = new HashMap<>();
    }

    /**
     * 获取业务方法的参数
     */
    public Object[] getArgs(){
        return args;
    }

    /**
     * 获取业务方法的返回值
     */
    public Object getResult(){
        return result;
    }

    /**
     * 设置业务方法的返回值
     */
    public void setResult(Object result){
        this.result = result;
    }

    /**
     * 获取自定义参数的值
     */
    public Object get(String key){
        return paramMap.get(key);
    }

    /**
     * 设置自定义参数的值,用于在不同功能之间传递自定义参数
     */
    public void put(String key,Object value){
        paramMap.put(key,value);
    }

}

获取springBean的工具类

package com.test.domi.common.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * spring上下文工具类
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {

    /**
     * spring应用上下文
     */
    private static ApplicationContext applicationContext;


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }

    /**
     * 获取Bean对象
     * @param calssz bean类
     * @return bean实例
     */
    public static <T> T getBean(Class<T> calssz){
        return applicationContext.getBean(calssz);
    }

    /**
     * 获取Bean对象
     * @param name beanName
     * @return bean对象
     */
    public static Object getBean(String name){
        return applicationContext.getBean(name);
    }

}
原文地址:https://www.cnblogs.com/domi22/p/9096247.html