이 나는 자바에서이 문제를 해결하는 방법입니다 (하지만 난 그게 스칼라에서 같은 추측) : 내가 문자열로 클래스를 번역하기 위해 JsonSerializer 클래스를 만든 다음 내가 번역 될 필드를 annote 내 수업과 함께 Json에.
는 날짜를 작동하는 방법을 보여하려는 exemple : (
public class MyClass
{
@Formats.DateTime(pattern="dd/MM/yyyy")
@JsonSerialize(using=JsonDateSerializer.class)
public Date myDate;
}
Ainsi, lorsque의 j'utilise의 mapper.writeValueAsString :
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 lst), j'obtiens des date au 형식 : 08-13-2014
소스를 Loiane Groner에서 복사했습니다.