如何在 spring boot 1.4.1-RELEASE 中设置球衣以将日期转换为 ISO 8601?

How to set jersey in spring boot 1.4.1-RELEASE to convert dates to ISO 8601?

在 Spring 引导 1.3.6-RELEASE 中,我将以下 class 注册到球衣。每个 java.util.Date 字段都将被读取并以 ISO8601 格式返回。但是,当更新到 1.4.1-RELEASE 时,它现在有时有效,有时无效。启用此功能的新正确方法是什么?

package com.mypackage;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.Date;

import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;
import javax.ws.rs.ext.Provider;

import com.fasterxml.jackson.databind.util.ISO8601Utils;

@Provider
public class DateTimeParamConverterProvider implements ParamConverterProvider {

    @SuppressWarnings("unchecked")
    @Override
    public <T> ParamConverter<T> getConverter(Class<T> clazz, Type type, Annotation[] annotations) {
        if (type.equals(Date.class)) {
            return (ParamConverter<T>) new DateTimeParamConverter();
        } else {
            return null;
        }
    }

    static class DateTimeParamConverter implements ParamConverter<Date> {
        @Override
        public java.util.Date fromString(String value) {
            if (value == null) {
                return null;
            }
            try {
                return ISO8601Utils.parse(value, new ParsePosition(0));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public String toString(Date value) {
            return ISO8601Utils.format(value);
        }
    }
}

我这样注册这个提供商:

@Component
@ApplicationPath("/")
public class JerseyConfiguration extends ResourceConfig {
    private static final Logger log = Logger.getLogger(JerseyConfiguration.class.getName());

    @Autowired
    public JerseyConfiguration(LogRequestFilter lrf) {
        register(new ObjectMapperContextResolverNonNull());
        register(RestServiceImpl.class);
        property(ServletProperties.FILTER_FORWARD_ON_404, true);
        register(DateTimeParamConverterProvider.class, 6000);
...

只需在 application.properties:

中定义
spring.jackson.date-format=com.fasterxml.jackson.databind.util.ISO8601DateFormat