1.自定义序列化过程
<1>方式1 参见http://www.cnblogs.com/lic309/p/5048631.htm 建议此方法
public class MyBeanSerializerModifier extends BeanSerializerModifier {
private JsonSerializer<Object> customeJsonSerializer = new NullSerializer();
private static final Set<String> IGNORED_FIELDS = new HashSet<String>(Arrays.asList("pwd", "id", "userName"));
@Override
public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc,
List<BeanPropertyWriter> beanProperties) {
// 循环所有的beanPropertyWriter
for (int i = 0; i < beanProperties.size(); i++) {
BeanPropertyWriter writer = beanProperties.get(i);
final String propName = writer.getName();
if (IGNORED_FIELDS.contains(propName)) {
writer.assignSerializer(this.defaultCustomeJsonSerializer());
}
}
return beanProperties;
}
private JsonSerializer<Object> defaultCustomeJsonSerializer() {
return customeJsonSerializer;
}
}
public class CustomeSerializer extends JsonSerializer<Object>{
@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
gen.writeString("********");
}
}
//调用
StringWriter stringWriter = new StringWriter();
try {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializerFactory(mapper.getSerializerFactory().withSerializerModifier(new MyBeanSerializerModifier()));
mapper.writeValue(stringWriter, paramObj);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stringWriter.toString();
<2>方式2
public class EntityCustomSerializationFactory extends BeanSerializerFactory {
private static final long serialVersionUID = 1L;
public EntityCustomSerializationFactory() {
this(null);
}
protected EntityCustomSerializationFactory(SerializerFactoryConfig config) {
super(config);
}
// ignored fields
private static final Set<String> IGNORED_FIELDS = new HashSet<String>(Arrays.asList("pwd", "id", "obj"));
@Override
protected void processViews(SerializationConfig config, BeanSerializerBuilder builder) {
super.processViews(config, builder);
// ignore fields only for concrete class
// note, that you can avoid or change this check
// if (builder.getBeanDescription().getBeanClass().equals(Entity.class))
// {
// get original writer
List<BeanPropertyWriter> originalWriters = builder.getProperties();
// create actual writers
List<BeanPropertyWriter> writers = new ArrayList<BeanPropertyWriter>();
for (BeanPropertyWriter writer : originalWriters) {
final String propName = writer.getName();
// if it isn't ignored field, add to actual writers list
if (IGNORED_FIELDS.contains(propName)) {
writer.assignSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
gen.writeString("****");
}
});
writers.add(writer);
} else {
writers.add(writer);
}
}
builder.setProperties(writers);
}
}
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializerFactory(new EntityCustomSerializationFactory());