接口定义的参数大小写问题解决

1、遇到实体的成员变量是大写的情况,而转换成JSON后首字母变成了小写,解决的办法是在实体的get方法上添加@JSONField(name = “XXX”)如:
@JsonProperty("OperatorID")
private String operatorId;

2、接口需求将接口返回或者接受的json字符串首字母大写:{"Name":"","小王":""},只需要在返回bean里面属性上加上@JsonProperty注解就可以,如:

@JsonProperty("OperatorID")
private String operatorId;

3、将首字母为大写json转换为特定的类的工具


import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.PropertyNameProcessor;

public class RequestUtils {
    /**
     * 将首字母为大写json转换为特定的类
     * @param clazz 目标类
     * @param jsonString json字符串
     * @return
     * @throws ClassNotFoundException
     */
    public static Object getJson(Class<?> clazz,String jsonString) throws ClassNotFoundException{
        JsonConfig config=new JsonConfig();
        config.setRootClass(clazz);
        Map<String,Class<?>> classMap = getListType(clazz);

        PropertyNameProcessor lowerCasePropertyNameProcessor = new PropertyNameProcessor() {
            @Override
            public String processPropertyName(Class aClass, String s) {
                return s.substring(0,1).toLowerCase() + s.substring(1);
            }
        };

        config.setClassMap(classMap);

        config.registerPropertyNameProcessor(clazz, lowerCasePropertyNameProcessor);
        for(Entry<String, Class<?>> entry : classMap.entrySet()){
            config.registerPropertyNameProcessor(entry.getValue(), lowerCasePropertyNameProcessor);
        }

        JSONObject jsonObject = JSONObject.fromObject(jsonString);
        Object bean = JSONObject.toBean(jsonObject, config);
        return bean;
    }

    /**
     * 利用反射获取属性中list类的属性名和泛型类型
     * @param clazz
     * @return
     * @throws ClassNotFoundException
     */
    public  static  Map<String, Class<?>> getListType(Class<?> clazz) throws ClassNotFoundException{
        Map<String, Class<?>> classmap = new HashMap<>();

        //通过反射获得list类的属性名和泛型类型
        Class<?> target = Class.forName(clazz.getName());
        Field[] fields = target.getDeclaredFields();
        for(Field f : fields){
            //判断是否为list类
            if(f.getType().isAssignableFrom(List.class)){
                //属性名
                String fieldName = f.getName();
                Type fc = f.getGenericType();
                if(fc instanceof ParameterizedType){
                    ParameterizedType pt = (ParameterizedType) fc;
                    //泛型类型
                    Class genericClazz = (Class)pt.getActualTypeArguments()[0]; //【4】 得到泛型里的class类型对象。

                    classmap.put(fieldName, genericClazz);
                    //递归调用此方法,获取属性类中的list
                    classmap.putAll(getListType(genericClazz));

                }

            }
        }
        return classmap;
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值