雪洗中关村

导航

fastJson + lombok + 属性名命名 踩坑点

 

JavaBean属性名要求:前两个字母要么都大写,要么都小写

 

package com.jdyh.worker.project.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;

// https://www.cnblogs.com/threecc/archive/2011/09/05/2167667.html     https://www.jb51.net/article/113074.htm
//lombok 与 手动生成getter的区别
public class dd3 {


    public static void main(String[] args) throws Exception {
        // java基础之内省(Introspector)操作javabean属性的理解(主要用于开发框架): http://blog.sina.com.cn/s/blog_e3bab1310102v7k3.html
        BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);
        PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
        for (int i = 0; i < pds.length; i++) {
            System.out.println(pds[i].getName());
        }
        Person person = new Person();
        person.setAA("1");
        person.setBb("2");
        person.setCC("3");//lombok
//        person.setcC("3");//手动生成getter
        String d2 = toJsonStr(person);
        System.out.println("---20-->" + d2);
        JSONObject d3 = toJson(d2);
        System.out.println("---21-->" + d3);
        Person d4 = jsontoBean(d3);
        System.out.println("---22-->" + d4.toString());

    }

    public static String toJsonStr(Object object) {
        return JSON.toJSONString(object);
    }

    public static JSONObject toJson(String str) {
        return JSON.parseObject(str);
    }

    public static Person jsontoBean(JSON json) {
        return JSON.toJavaObject(json, Person.class);
    }

    @Data
    static class Person {//这是一个JavaBean
        private String AA;
        private String Bb;
        private String cC;

        public String getAA() {
            return AA;
        }

        public void setAA(String aa) {
            AA = aa;
        }

        public String getBb() {//decapitalize(Bb)会得出属性是bb,实际是Bb
            return Bb;
        }

        public void setBb(String bb) {
            Bb = bb;
        }

        public String getCC() {//decapitalize(CC)会得出属性是CC,实际是cC
            return cC;
        }

        public void setCC(String cc) {
            cC = cc;
        }

    }
}

 

posted on 2019-05-11 13:18  雪洗中关村  阅读(4788)  评论(0编辑  收藏  举报