• <bdo id='TgyfS'></bdo><ul id='TgyfS'></ul>

      <small id='TgyfS'></small><noframes id='TgyfS'>

      <tfoot id='TgyfS'></tfoot>

        <legend id='TgyfS'><style id='TgyfS'><dir id='TgyfS'><q id='TgyfS'></q></dir></style></legend>

      1. <i id='TgyfS'><tr id='TgyfS'><dt id='TgyfS'><q id='TgyfS'><span id='TgyfS'><b id='TgyfS'><form id='TgyfS'><ins id='TgyfS'></ins><ul id='TgyfS'></ul><sub id='TgyfS'></sub></form><legend id='TgyfS'></legend><bdo id='TgyfS'><pre id='TgyfS'><center id='TgyfS'></center></pre></bdo></b><th id='TgyfS'></th></span></q></dt></tr></i><div id='TgyfS'><tfoot id='TgyfS'></tfoot><dl id='TgyfS'><fieldset id='TgyfS'></fieldset></dl></div>
      2. 自定义 Jackson ObjectMapper 以读取自定义注释并屏蔽

        时间:2023-09-27
          <bdo id='4WhQF'></bdo><ul id='4WhQF'></ul>
            • <i id='4WhQF'><tr id='4WhQF'><dt id='4WhQF'><q id='4WhQF'><span id='4WhQF'><b id='4WhQF'><form id='4WhQF'><ins id='4WhQF'></ins><ul id='4WhQF'></ul><sub id='4WhQF'></sub></form><legend id='4WhQF'></legend><bdo id='4WhQF'><pre id='4WhQF'><center id='4WhQF'></center></pre></bdo></b><th id='4WhQF'></th></span></q></dt></tr></i><div id='4WhQF'><tfoot id='4WhQF'></tfoot><dl id='4WhQF'><fieldset id='4WhQF'></fieldset></dl></div>

              <small id='4WhQF'></small><noframes id='4WhQF'>

                  <tbody id='4WhQF'></tbody>

                  <legend id='4WhQF'><style id='4WhQF'><dir id='4WhQF'><q id='4WhQF'></q></dir></style></legend>
                  <tfoot id='4WhQF'></tfoot>
                  本文介绍了自定义 Jackson ObjectMapper 以读取自定义注释并屏蔽注释字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个要求,我创建了一个自定义注释@MaskSensitiveData.我注释敏感字段.喜欢

                  I have a requirement where I have created a Custom Annotation @MaskSensitiveData. I annotate sensitive fields. like

                  class MyBean {
                      String userName;
                      @MaskSensitiveData
                      String cardNumber;
                      String abc;
                      String xyz;
                  }
                  
                  ObjectMapper mapper = new ObjectMapper();
                      String json = null;
                      AnnotationIntrospector primary = new JaxbAnnotationIntrospector();
                      AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();
                      AnnotationIntrospector pair = new AnnotationIntrospectorPair(primary, secondary);
                      mapper.setAnnotationIntrospector(pair);
                      try {
                          json = mapper.writeValueAsString(obj);
                          /*
                           * if(json != null ) { json = getLoggableString(json); }
                           */
                      } catch (Exception e) {
                          return "Unable to convert to Json object:" + obj.toString() + " Message: " + e.getMessage();
                  
                      }
                  

                  我正在使用 Jackson ObjectMapper 将 objct 转换为 Json 之类的.我需要自定义 Object Mapper 以在返回 json 中屏蔽 cardNumber 字段.请提出更好的方法.

                  I am using Jackson ObjectMapper to convert objct to Json like. I need to customize Object Mapper to mask cardNumber field in return json. Please suggest a better way.

                  推荐答案

                  package stackoverflow;
                  
                  import static org.hamcrest.MatcherAssert.assertThat;
                  import static org.hamcrest.Matchers.is;
                  
                  import java.io.IOException;
                  import java.lang.annotation.Retention;
                  import java.lang.annotation.RetentionPolicy;
                  
                  import org.hamcrest.Matchers;
                  import org.junit.Test;
                  
                  import com.fasterxml.jackson.core.JsonGenerator;
                  import com.fasterxml.jackson.core.JsonParser;
                  import com.fasterxml.jackson.core.JsonProcessingException;
                  import com.fasterxml.jackson.databind.AnnotationIntrospector;
                  import com.fasterxml.jackson.databind.DeserializationContext;
                  import com.fasterxml.jackson.databind.ObjectMapper;
                  import com.fasterxml.jackson.databind.SerializerProvider;
                  import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
                  import com.fasterxml.jackson.databind.introspect.Annotated;
                  import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair;
                  import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector;
                  import com.fasterxml.jackson.databind.ser.std.StdSerializer;
                  
                  public class MaskingAnnotationExample {
                      // Define @custom Annotation
                      // assumed to be used by String type field for this example
                      @Retention(RetentionPolicy.RUNTIME)
                      static @interface MaskSensitiveData {
                      }
                  
                      public static class MyBean {
                          private String userName;
                  
                          @MaskSensitiveData
                          private String cardNumber;
                  
                          public MyBean() {
                          }
                  
                          public String getCardNumber() {
                              return cardNumber;
                          }
                  
                          public String getUserName() {
                              return userName;
                          }
                  
                          public void setUserName(String userName) {
                              this.userName = userName;
                          }
                  
                          public void setCardNumber(String cardNumber) {
                              this.cardNumber = cardNumber;
                          }
                      }
                  
                      // map the Serializer/Deserializer based on custom annotation
                      public static class MaskSensitiveDataAnnotationIntrospector extends NopAnnotationIntrospector {
                          private static final long serialVersionUID = 1L;
                  
                          @Override
                          public Object findSerializer(Annotated am) {
                              MaskSensitiveData annotation = am.getAnnotation(MaskSensitiveData.class);
                              if (annotation != null) {
                                  return MaskSensitiveDataSerializer.class;
                              }
                  
                              return null;
                          }
                  
                          @Override
                          public Object findDeserializer(Annotated am) {
                              MaskSensitiveData annotation = am.getAnnotation(MaskSensitiveData.class);
                              if (annotation != null) {
                                  return MaskSensitiveDataDeserializer.class;
                              }
                  
                              return null;
                          }
                      }
                  
                      public static class MaskSensitiveDataDeserializer extends StdDeserializer<String> {
                          private static final long serialVersionUID = 1L;
                  
                          public MaskSensitiveDataDeserializer() {
                              super(String.class);
                          }
                  
                          @Override
                          public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
                              // un-masking logic here. in our example we are removing "MASK"
                              // string
                              String s = p.getValueAsString();
                              return s.substring(4);
                          }
                      }
                  
                      public static class MaskSensitiveDataSerializer extends StdSerializer<String> {
                          private static final long serialVersionUID = 1L;
                  
                          public MaskSensitiveDataSerializer() {
                              super(String.class);
                          }
                  
                          @Override
                          public void serialize(String value, JsonGenerator gen, SerializerProvider provider) throws IOException {
                              // Masking data; for our example we are adding 'MASK'
                              gen.writeString("MASK" + value);
                          }
                      }
                  
                      @Test
                      public void demo() throws Exception {
                          ObjectMapper mapper = new ObjectMapper();
                  
                          AnnotationIntrospector sis = mapper.getSerializationConfig().getAnnotationIntrospector();
                          AnnotationIntrospector dis = mapper.getDeserializationConfig().getAnnotationIntrospector();
                  
                          AnnotationIntrospector is1 = AnnotationIntrospectorPair.pair(sis, new MaskSensitiveDataAnnotationIntrospector());
                          AnnotationIntrospector is2 = AnnotationIntrospectorPair.pair(dis, new MaskSensitiveDataAnnotationIntrospector());
                  
                          mapper.setAnnotationIntrospectors(is1, is2);
                  
                          MyBean obj = new MyBean();
                          obj.setUserName("Saurabh Bhardwaj");
                          obj.setCardNumber("4455-7788-9999-7777");
                          String json = mapper.writeValueAsString(obj);
                  
                          String expectedJson = "{"userName":"Saurabh Bhardwaj","cardNumber":"MASK4455-7788-9999-7777"}";
                          assertThat(json, Matchers.is(expectedJson));
                  
                          MyBean cloned = mapper.readValue(json, MyBean.class);
                          assertThat(cloned.getCardNumber(), is(obj.getCardNumber()));
                      }
                  }
                  

                  希望这会有所帮助.

                  这篇关于自定义 Jackson ObjectMapper 以读取自定义注释并屏蔽注释字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何基于 URL 模式应用 Spring Boot 过滤器? 下一篇:如何在 Spring @Value 注解中正确指定默认值?

                  相关文章

                  最新文章

                    <small id='LSugR'></small><noframes id='LSugR'>

                    • <bdo id='LSugR'></bdo><ul id='LSugR'></ul>
                    <i id='LSugR'><tr id='LSugR'><dt id='LSugR'><q id='LSugR'><span id='LSugR'><b id='LSugR'><form id='LSugR'><ins id='LSugR'></ins><ul id='LSugR'></ul><sub id='LSugR'></sub></form><legend id='LSugR'></legend><bdo id='LSugR'><pre id='LSugR'><center id='LSugR'></center></pre></bdo></b><th id='LSugR'></th></span></q></dt></tr></i><div id='LSugR'><tfoot id='LSugR'></tfoot><dl id='LSugR'><fieldset id='LSugR'></fieldset></dl></div>
                  1. <tfoot id='LSugR'></tfoot>
                    <legend id='LSugR'><style id='LSugR'><dir id='LSugR'><q id='LSugR'></q></dir></style></legend>