自定义接口参数Formatter - xinwu-yang/cube-java GitHub Wiki

Since 2.6.0

针对Form表单提交的参数Jackson不会去处理,此时需要我们定义一些Formatter来转换。

如何自定义

实现org.springframework.format.Formatter接口并注入到Spring容器

定义Formatter

如下:实现了表单提交时间字符串转化接口参数为Date的示例

@Component
public class DateFormatter implements Formatter<Date> {
    @Override
    public Date parse(String text, Locale locale) throws ParseException {
        return DateUtils.parseDate(text, "yyyy-MM-dd HH:mm:ss");
    }

    @Override
    public String print(Date object, Locale locale) {
        return DateUtils.formatDate(object, "yyyy-MM-dd HH:mm:ss");
    }
}
⚠️ **GitHub.com Fallback** ⚠️