Grails日期解组 [英] Grails Date unmarshalling
问题描述
如果我从RESTful客户端获得以下json,如何优雅地解组java.util.Date? (是否有可能不提供(也就是硬编码)格式,这就是我的意思是优雅的...)
{
class:url,
link:http://www.empa.ch,
rating:5,
lastcrawl :2009-06-04 16:53:26.706 CEST,
checksum:837261836712xxxkfjhds,
}
最简洁的方式可能是为可能的日期格式注册自定义DataBinder。
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CustomDateBinder extends PropertyEditorSupport {
private final List< String>格式;
public CustomDateBinder(列表格式){
List< String> formatList = new ArrayList< String>(formats.size());
for(Object format:formats){
formatList.add(format.toString()); //强制字符串值(例如,对于GStrings)
}
this.formats = Collections.unmodifiableList(formatList);
$ b @Override
public void setAsText(String s)throws IllegalArgumentException {
if(s!= null)
for(String format:formats ){
//每次都需要创建SimpleDateFormat,因为它不是安全的
SimpleDateFormat df = new SimpleDateFormat(format);
try {
setValue(df.parse(s));
return;
} catch(ParseException e){
//忽略
}
}
}
}
您还需要实现PropertyEditorRegistrar
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
导入grails.util.GrailsConfig;
import java.util.Date;
import java.util.List;
public class CustomEditorRegistrar实现PropertyEditorRegistrar {
public void registerCustomEditors(PropertyEditorRegistry reg){
reg.registerCustomEditor(Date.class,new CustomDateBinder(GrailsConfig.get(grails.date。格式,List.class)));
$ p $并且在你的grails中创建一个Spring bean定义, app / conf / spring / resources.groovy:
beans = {
customEditorRegistrar(CustomEditorRegistrar)
}
最后在您的grails-app / conf / Config.groovy中定义日期格式:
grails.date.formats = [yyyy-MM-dd HH:mm:ss.SSS ZZZZ,dd.MM .yyyy HH:mm:ss]
If I get the following json from a RESTful client, how do I elegantly unmarshal the java.util.Date? (Is it possible without providing (aka. hard-coding) the format, that's what I mean by elegantly...)
{
"class": "url",
"link": "http://www.empa.ch",
"rating": 5,
"lastcrawl" : "2009-06-04 16:53:26.706 CEST",
"checksum" : "837261836712xxxkfjhds",
}
解决方案 The cleanest way is probably to register a custom DataBinder for possible date formats.
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CustomDateBinder extends PropertyEditorSupport {
private final List<String> formats;
public CustomDateBinder(List formats) {
List<String> formatList = new ArrayList<String>(formats.size());
for (Object format : formats) {
formatList.add(format.toString()); // Force String values (eg. for GStrings)
}
this.formats = Collections.unmodifiableList(formatList);
}
@Override
public void setAsText(String s) throws IllegalArgumentException {
if (s != null)
for (String format : formats) {
// Need to create the SimpleDateFormat every time, since it's not thead-safe
SimpleDateFormat df = new SimpleDateFormat(format);
try {
setValue(df.parse(s));
return;
} catch (ParseException e) {
// Ignore
}
}
}
}
You'd also need to implement a PropertyEditorRegistrar
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import grails.util.GrailsConfig;
import java.util.Date;
import java.util.List;
public class CustomEditorRegistrar implements PropertyEditorRegistrar {
public void registerCustomEditors(PropertyEditorRegistry reg) {
reg.registerCustomEditor(Date.class, new CustomDateBinder(GrailsConfig.get("grails.date.formats", List.class)));
}
}
and create a Spring-bean definition in your grails-app/conf/spring/resources.groovy:
beans = {
"customEditorRegistrar"(CustomEditorRegistrar)
}
and finally define the date formats in your grails-app/conf/Config.groovy:
grails.date.formats = ["yyyy-MM-dd HH:mm:ss.SSS ZZZZ", "dd.MM.yyyy HH:mm:ss"]
这篇关于Grails日期解组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!