@JsonSerialize(using = MyCustomSerializer.class未按预期工作

时间:2015-08-18 15:50:01

标签: java json jackson

我有一个App Entity,其中包含一些参数,如CustomerName,Address和CardNumber。在将杰克逊与JSON串行化的同时,我希望屏蔽卡号。例如,如果CardNumber是12345678901234,我希望JSON的值为CardNumber:1234 ****** 1234。所以我在Card Number属性上添加了@JsonSerialize注释,并使其使用我的自定义序列化程序类。但这似乎并没有奏效。

实体类

  import javax.xml.bind.annotation.XmlAccessType;
  import javax.xml.bind.annotation.XmlAccessorType;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlRootElement;
  import javax.xml.bind.annotation.XmlType;

  import org.codehaus.jackson.map.annotate.JsonSerialize;

  @XmlRootElement
  @XmlAccessorType(XmlAccessType.PROPERTY)
  @XmlType(name = "TransactionDetailsType", propOrder = { "CustomerName", "Address", "CardNumber" })
  public class App {

      @XmlElement(name = "Address")
      private String address;

      @JsonSerialize(using = CardNumberMaskingSerializer.class)
      @XmlElement(name = "CardNumber")
      private String cardNumber;

      @XmlElement(name = "CustomerName")
      private String customerName;

      public String getAddress() {
          return address;
      }

      public String getCardNumber() {
          return cardNumber;
      }

      public String getCustomerName() {
          return customerName;
      }

      public void setAddress(String address) {
        this.address = address;
      }

        public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
       }

       public void setCustomerName(String customerName) {
           this.customerName = customerName;
       }

   }

自定义序列化程序类

public class CardNumberMaskingSerializer extends JsonSerializer<String> {

    private final static String MASK_CHAR = "*";

    @Override
    public void serialize(String cardNumber, JsonGenerator jgen, SerializerProvider provider) throws IOException,
            JsonProcessingException {
        String s = cardNumber.replaceAll("\\D", "");
        System.out.println(s);
        int start = 4;
        int end = s.length() - 4;
        String overlay = StringUtils.repeat(MASK_CHAR, end - start);
        String maskedNumber = StringUtils.overlay(s, overlay, start, end);
        System.out.println(maskedNumber);
        jgen.writeString(maskedNumber);
    }

}

Marshaller Class

 import java.io.IOException;
 import java.io.UnsupportedEncodingException;

 import org.apache.commons.io.output.ByteArrayOutputStream;
 import org.codehaus.jackson.map.AnnotationIntrospector;
 import org.codehaus.jackson.map.ObjectMapper;
 import org.codehaus.jackson.xc.JaxbAnnotationIntrospector;

 public class AppMarshaller {

     private final ObjectMapper mapper;

     public AppMarshaller() {
         mapper = new ObjectMapper();

         AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();

              mapper.setDeserializationConfig(mapper.getDeserializationConfig().withAnnotation     Introspector(introspector));
         mapper.setSerializationConfig(mapper.getSerializationConfig().withAnnotationIntr     ospector(introspector));
     }


public App read(String jsonText) {
    try {
        return mapper.readValue(jsonText, App.class);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}


public String write(App details) {

    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();

    try {
        mapper.setSerializationConfig(mapper.getSerializationConfig().withAnnotationIntrospector(
                new JaxbAnnotationIntrospector()));

        mapper.writeValue(byteStream, details);

    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    try {
        return byteStream.toString("UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

}

但是当我尝试在JUNIT中测试它时,通过调用marshaller.write(app),自定义序列化程序没有被调用。所以我没有得到预期的输出。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

当我发现自己与杰克逊注释和JAXB注释混淆时,我解决了这个问题。 @XmlElement是jaxb,@ JsonSerialize是jackson。

我写了一个自定义的jaxb marshaller amd解决了这个问题。