spring-boot 自定義反序列化 @JsonDeserialize

[Java] 通過反射,動態修改註解的某個屬性值

https://segmentfault.com/a/1190000011213222  //很不錯哦, 通過反射,動態修改註解的某個屬性值

注意反射修改某個屬性的值,最好使用自己的filed;如代碼:


@Slf4j
public class Deserialize extends JsonDeserializer<bean> {
  @Autowired
  private Deserialize deserialize;
  @Override
  public bean deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException {
    ObjectCodec oc = jp.getCodec();
    JsonNode node = oc.readTree(jp);
    try {
        // 調用反序列化方法,不能調用系統默認的(或者本bean的),會造成循環依賴
      bean = deserialize.func(node, bean);
      return bean;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
}

 


import com.fasterxml.jackson.databind.JsonNode;
import com.lucrativ.microservice.contact.annotation.FieldDefault;
import com.lucrativ.microservice.util.BeanUtil;

import org.apache.poi.ss.formula.functions.T;
import org.jetbrains.annotations.NotNull;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Deserialize {

  /**
   *
   * @param oObj
   * @param tObj
   * @param <T>
   * @return
   */
  public <T> T getUpdateReferenceVo(T oObj, T tObj, T fObj) {
    Field[] fields = oObj.getClass().getDeclaredFields();
    BeanUtil.copyProperties(oObj, tObj);
    for (Field field : fields) {
      try {
        field.setAccessible(true);
        FieldDefault fieldDefault = field.getAnnotation(FieldDefault.class);
        InvocationHandler h = Proxy.getInvocationHandler(fieldDefault);
        Field hField = h.getClass().getDeclaredField("memberValues");
        hField.setAccessible(true);
        Map memberValues = (Map) hField.get(h);
        Object isClear = memberValues.get("isClear");
        if ((boolean)isClear) {
          String name = field.getType().getName();
          Field f = fObj.getClass().getDeclaredField(field.getName());
          f.setAccessible(true);
          Field t = tObj.getClass().getDeclaredField(field.getName());
          t.setAccessible(true);
          if (String.class.getName().equals(name)) {
            t.set(tObj, "");
//            f.set(fObj, "");
          }
          if (Integer.class.getName().equals(name)
            || Double.class.getName().equals(name) || Float.class.getName().equals(name)
            || Long.class.getName().equals(name)) {
            t.set(tObj, null);
            f.set(fObj, null);
          }
          if (int.class.getName().equals(name) || long.class.getName().equals(name)) {
            t.set(tObj, 0);
            f.set(fObj, 0);
          }
        }
      } catch (Exception e) {
        System.out.println(e);
      }
    }
    return tObj;
  }

  /**
   *
   * @param node
   * @param tObj
   * @param <T>
   * @return
   */
  public <T>T func(JsonNode node, @NotNull T tObj) {
    Field[] fields =  tObj.getClass().getDeclaredFields();
    for (Field field : fields) {
      JsonNode fieldNode = node.findValue(field.getName());
      try {
        boolean isSetNull = fieldNode.isNull();
        if (isSetNull) {
          field.setAccessible(true);
          String name = field.getType().getName();
          if (String.class.getName().equals(name)) {
            Field f = tObj.getClass().getDeclaredField(field.getName());
            f.setAccessible(true);
            f.set(tObj, "");
          }
          FieldDefault fieldDefault = field.getAnnotation(FieldDefault.class);
          InvocationHandler h = Proxy.getInvocationHandler(fieldDefault);
          Field hField = h.getClass().getDeclaredField("memberValues");
          hField.setAccessible(true);
          Map memberValues = (Map) hField.get(h);
          memberValues.put("isClear", true);
        }
      } catch (Exception e) {

      }
    }
    return tObj;
  }

  public void getFieldAnnotation(Class<T> tClass) {
    Field[] fields = tClass.getClass().getDeclaredFields();
    for (Field field : fields) {
      Annotation[] allFAnnos= field.getAnnotations();
      Annotation[] deFAnnos = field.getDeclaredAnnotations();
      Annotation annotation = field.getAnnotation(FieldDefault.class);
//      Annotation annotation1 = field.getAnnotation(Field.class);
      // 輸出註解上的屬性
//      annotation.getis
      System.out.println("**************************************************\n");
    }
  }

}

 

@JsonDeserialize(using = Deserialize.class)
public class Bean extends beanP {

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章