Spring RESTful日期格式

时间:2016-04-07 00:59:11

标签: json spring rest jackson

像jdk8这样的休息控制器

@Transactional
@RequestMapping(value="/A", method = RequestMethod.POST)
public ABean aInsert(final java.sql.Date when, final long company) {
  A bean = new A();
  bean.setWhen(when);
  bean.setCompany(entityManager.find(Company.class, company));
  entityManager.persist(bean);
  return new ABean(bean.getId(), bean.getVersion(), bean.getWhen(), bean.getCompany().getId());
}
使用表单后期数据

调用

when=1459987200000&company=1

但无法调用并返回400 - bad request (description The request sent by the client was syntactically incorrect.)

我的问题是:

  • java.sql.date的默认输入字符串格式是什么?

1 个答案:

答案 0 :(得分:0)

when=2016-04-21&company=10有效,所以

yyyy-mm-dd

对于其他每种格式,请注册一个自定义Converter,如下所示:

@Autowired
private final ConfigurableConversionService conversionService=null;

@PostConstruct
public void registerConverter() {
    conversionService.addConverter(new Converter<String, Long>() {
        @Override
        public Long convert(String source) {
            try {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd'T'hh:mm");
                return df.parse(source).getTime();
            } catch (ParseException e) {
                return Long.parseLong(source);
            }
        }
    });
}