只要继承它的抽象类:public abstract class JsonSerializer<T>,并在相应的属性方法上添加指定注解:@JsonSerialize 即可实现。 编写Date日志自定义转换类: CustomDateSerializer.java import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.JsonProcessingException; import org.codehaus.jackson.map.JsonSerializer; import org.codehaus.jackson.map.SerializerProvider; public class CustomDateSerializer extends JsonSerializer<Date> { @Override public void serialize(Date value, JsonGenerator jgen,SerializerProvider provider) throws IOException,JsonProcessingException { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formattedDate = formatter.format(value); jgen.writeString(formattedDate); } }
javabean对应get方法添加注解:
@JsonSerialize(using = CustomDateSerializer.class) public Date getCreateDate() { return createDate; }
时间: 2024-12-09 18:09:54