java序列化对象,根据不同配置动态改变属性名
java序列化对象,根据不同配置动态改变属性名
使用场景:自己项目对接多个外部系统,各个外部系统使用的字段并没有统一,所以要根据不同系统动态的输出序列化数据,使适应各个系统的要求
实现方式
使用自定义注解和fastjson实现需求
fastjson的NameFilter的作用:序列化时,属性名变成自己指定的名称
实现过程
自定义注解一:MyParamName
package xin.yangshuai.cipher.json;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** * MyParamName * *
@author shuai *
@date 2022/7/2 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
public @interface MyParamName
{
String value() default "";
NameEle[] names() default {};
}
value、names都是非必填
value:默认序列化的名称
names:自定义注解二,数组形式
自定义注解二:NameEle
package xin.yangshuai.cipher.json;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** * NameEle * * @author shuai *
@date 2021/3/24 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
public
@interface NameEle {
String id();
String value();
}
id:唯一标识,确定序列化后的属性名
value:定义序列化后的属性名
手写MyNameFilter,实现fastjson的NameFilter
package xin.yangshuai.cipher.json;
import com.alibaba.fastjson.serializer.NameFilter;
import java.lang.reflect.Field;/** * MyNameFilter * *
@author shuai *
@date 2021/3/23 */public class MyNameFilter implements NameFilter {
private String id;
public MyNameFilter() { }
public MyNameFilter(String id)
{
this.id = id; }
@Override
public String process(Object o, String name, Object value)
{
try
{
Field field = o.getClass().getDeclaredField(name);
MyParamName myParamName = field.getDeclaredAnnotation(MyParamName.class);
NameEle[] nameEles = myParamName.names();
String defaultName = myParamName.value();
if (nameEles.length == 0 && defaultName.length() > 0)
{
return defaultName;
}
for (NameEle n : nameEles)
{
if (id.equals(n.id()))
{
return n.value();
}
}
}
catch (Exception e)
{
return name;
}
return name;
}
}
构造方法传递id属性的值,与自定义注解二的id进行匹配,匹配成功则使用对应的属性名
实体类,属性上添加自定义注解
package xin.yangshuai.cipher.json;import com.alibaba.fastjson.JSON;/** * Person * * @author shuai * @date 2022/7/2 */public class Person
{
@MyParamName(names =
{
@NameEle(id = "ln", value = "i_d"),
@NameEle(id = "jl", value = "_id")})
private String id;
@MyParamName("NAME")
private String name;
private String age;
public String getId()
{
return id; }
public void setId(String id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAge()
{
return age;
}
public void setAge(String age)
{
this.age = age;
}
@Override
public String toString()
{
return "Person{" + "id='" + id + ''' + ", name='" + name + ''' + ", age='" + age + ''' + '}';
}
}
运行主方法测试
public static void main(String[] args)
{
Person person = new Person();
person.setAge("18");
person.setId("001");
person.setName("张三");
String s = JSON.toJSONString(person, new MyNameFilter("jl"));
System.out.println(s);
String s1 = JSON.toJSONString(person, new MyNameFilter("ln"));
System.out.println(s1);}
结果为:
{"age":"18","_id":"001","NAME":"张三"} {"age":"18","i_d":"001","NAME":"张三"}