【问题标题】:Spring RESTful Date formatSpring RESTful 日期格式
【发布时间】:2016-04-07 00:59:11
【问题描述】:

jdk8中这样的一个rest控制器

@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 的默认输入字符串格式是什么?

【问题讨论】:

    标签: json spring rest jackson


    【解决方案1】:

    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);
                }
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-21
      • 2017-04-19
      • 2012-02-20
      • 2013-02-01
      • 2013-07-19
      • 2016-05-10
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多