2009-06-08 33 views
13

如果我从RESTful客户端获得以下json,如何优雅地解组java.util.Date? (是否有可能不提供(亦称硬编码)格式,这就是我所说的优雅...)Grails日期解组

{ 
    "class": "url", 
    "link": "http://www.empa.ch", 
    "rating": 5, 
    "lastcrawl" : "2009-06-04 16:53:26.706 CEST", 
    "checksum" : "837261836712xxxkfjhds", 
} 

回答

18

最彻底的方法可能是注册可能的日期格式自定义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> 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 
       } 
      } 
    } 
} 

你想也需要实现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))); 
    } 
}   

,并在您的grails-app/conf目录/春/ resources.groovy创建一个Spring bean定义:

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"] 
+0

想知道是否有一个原因,你会选择在Java(如上),而不是Groovy中实现这个? Groovy的代码会比较短。 – 2009-10-22 00:42:25

+0

我以前在Groovy比现在慢得多时,在Java中实现了一段类似的代码。 Groovy在这件事上做了一个巨大的飞跃。我只是懒惰的重用旧的Java代码;-) – 2009-10-22 09:42:04

5

请注意,Grails 2.3+的新版本支持此类功能。 见Date Formats for Data Binding

随着中说,如果你被迫2.3之前,使用一个版本的Grails中,CustomEditorRegistrar 可以使用下面的代码,以消除弃用警告进行更新,并且还使用了@Component注释,这使得您可以删除/跳过直接在resources.groovy中添加bean的步骤。 也不是我将grails配置属性名称更改为grails.databinding.dateFormats,它与Grails 2.3+中现在支持的属性相匹配。最后,我的版本是.groovy,而不是.java文件。

import javax.annotation.Resource 
import org.codehaus.groovy.grails.commons.GrailsApplication 
import org.springframework.beans.PropertyEditorRegistrar 
import org.springframework.beans.PropertyEditorRegistry 
import org.springframework.stereotype.Component 

@Component 
public class CustomEditorRegistrar implements PropertyEditorRegistrar { 

    @Resource 
    GrailsApplication grailsApplication 

    public void registerCustomEditors(PropertyEditorRegistry reg){ 
     def dateFormats = grailsApplication.config.grails.databinding.dateFormats as List 
     reg.registerCustomEditor(Date.class, new CustomDateBinder(dateFormats)) 
    } 
}