【注解】@JsonIgnoreProperties 指示在序列化和反序列化过程中要忽略的属性。

@JsonIgnoreProperties 是 Jackson 库中的注解,用于指示在序列化和反序列化过程中要忽略的属性。

具体来说,@JsonIgnoreProperties 可以用在类级别属性级别上。

1、类级别的使用:

如果你在整个类上使用 @JsonIgnoreProperties 注解,可以指定要在序列化和反序列化中忽略的属性列表。这样,这些属性在 JSON 转换过程中就会被忽略。

@JsonIgnoreProperties({"ignoreField1", "ignoreField2"})
public class MyClass {
    // 类的定义
}

也可以直接使用 ignoreUnknown = true 来忽略JSON字符串中的任何未知属性,即在MyClass类中没有对应字段的任何属性都将被忽略。

@JsonIgnoreProperties(ignoreUnknown = true)
class MyClass{
  private String title;
  private int price; 
  ..
 
}

2、属性级别的使用:

如果你只想在特定属性上使用 @JsonIgnoreProperties,可以在属性上直接添加该注解,指定要忽略的属性。

public class MyClass {
    @JsonIgnoreProperties
    private String ignoreField;
    
    // 其他属性和方法
}

这个注解在实际项目中常用于忽略一些不需要被序列化或反序列化的属性,以达到定制化 JSON 转换的目的。例如,有些字段可能只在服务端使用,不需要传递给客户端,可以通过这个注解进行配置

持续更新中,点赞——>收藏——>关注,
@JsonIgnoreProperties(ignoreUnknown = true) 忽略多余的,不包括你😀

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对您的问题,我可以给出以下实现步骤: 1. 定义注解类,用于标记需要序列化反序列化的字段。 2. 定义序列化器和反序列化器,使用反射获取被注解标记的字段,并将其转换为字符串或从字符串转换回来。 3. 在需要进行序列化反序列化的类中使用注解标记需要进行操作的字段。 以下是一个简单的示例代码: ```java import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.Field; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) @interface Serialize { } class Serializer { public static String serialize(Object obj) throws IllegalAccessException { StringBuilder sb = new StringBuilder(); for (Field field : obj.getClass().getDeclaredFields()) { if (field.isAnnotationPresent(Serialize.class)) { field.setAccessible(true); sb.append(field.getName()).append("=").append(field.get(obj)).append(";"); } } return sb.toString(); } } class Deserializer { public static void deserialize(Object obj, String str) throws NoSuchFieldException, IllegalAccessException { for (String fieldString : str.split(";")) { String[] parts = fieldString.split("="); Field field = obj.getClass().getDeclaredField(parts[0]); if (field.isAnnotationPresent(Serialize.class)) { field.setAccessible(true); if (field.getType() == int.class) { field.setInt(obj, Integer.parseInt(parts[1])); } else if (field.getType() == String.class) { field.set(obj, parts[1]); } } } } } class Person { @Serialize private String name; @Serialize private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String toString() { return "Person{name='" + name + "', age=" + age + "}"; } } public class Main { public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException { Person person = new Person("Alice", 30); String serialized = Serializer.serialize(person); System.out.println(serialized); Person deserialized = new Person("", 0); Deserializer.deserialize(deserialized, serialized); System.out.println(deserialized); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值