我有一个带有某些参数的应用程序实体,例如CustomerName,Address和CardNumber。在用Jackson将其序列化为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);
}
}
马歇尔班
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);
}
}
}
但是,当我尝试通过调用marshaller.write(app)在JUNIT中对其进行测试时,未调用自定义序列化程序。所以我没有得到预期的输出。有人可以帮忙吗?
最佳答案
当我发现自己将Jackson注释和JAXB注释混淆时,我解决了此问题。 @XmlElement是jaxb,@ JsonSerialize是jackson。
我写了一个自定义的jaxb marshaller amd解决了这个问题。