这是我如何解决在Java这个问题(但我想它是Scala中一样): 我创建了一个JsonSerializer类一类转换成字符串,然后我annote将被翻译领域与我的班级一起进入Json。
的为例向您展示它是如何的日期工作:
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.stereotype.Component;
/**
* Used to serialize Java.util.Date, which is not a common JSON
* type, so we have to create a custom serialize method;.
*
* @author Loiane Groner
*/
@Component
public class JsonDateSerializer extends JsonSerializer<Date>{
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
@Override
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
throws IOException, JsonProcessingException {
String formattedDate = dateFormat.format(date);
gen.writeString(formattedDate);
}
}
然后我annote相应的字段我的同班同学:
public class MyClass
{
@Formats.DateTime(pattern="dd/MM/yyyy")
@JsonSerialize(using=JsonDateSerializer.class)
public Date myDate;
}
AINSI,lorsque j'utilise mapper.writeValueAsString( LST),j'obtiens DES日期AU格式为:2014年8月13日
我从Loiane Groner复制的来源。