相关注解
@JsonSerialize(using = ToStringSerializer.class)
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")
序列化
jackson
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategy.SNAKE_CASE);//驼峰
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);//空值不序列化
try {
mapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
//字段上使用该自定义转换类
@JsonSerialize(using = DataSerializerUtils.class)
private BigDecimal xxx;
/**
* 转json时,小数保留两位小数
*/
public class DataSerializerUtils extends JsonSerializer<Object>{
@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (value != null) {
DecimalFormat df = new DecimalFormat("0.00");
df.setRoundingMode(RoundingMode.HALF_UP);
String _value = df.format(value);
gen.writeString(_value);
} else {
gen.writeString("0.00");
}
}
}