14

如果我从 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",
}
4

2 回答 2

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/spring/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"]
于 2009-06-08T09:37:51.360 回答
5

请注意,新版本的 Grails 2.3+ 开箱即用地支持这种类型的功能。请参阅数据绑定的日期格式

话虽如此,如果您被迫使用 2.3 之前的 Grails 版本,CustomEditorRegistrar 可以使用以下代码进行更新以消除弃用警告,并使用@Component注释,它允许您删除/跳过添加步骤直接入豆resources.groovy。也不是我将 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))
    }
}
于 2014-06-12T14:47:15.773 回答