Modelmapper从String转换为LocalDate [英] Modelmapper to convert from String to LocalDate

查看:563
本文介绍了Modelmapper从String转换为LocalDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的DTO具有字符串格式的日期字段.我的实体的日期为LocalDate.目前,我正在从地图上跳过它,然后稍后手动将其显式设置(将String设置为Date,反之亦然).

My DTO is having date field in String format. My entity is having date as LocalDate. Currently I am skipping it from map and then later manually explicitly setting it (String to Date and vis-versa).

是否可以自动转换?我在spring bean中尝试了Converter,但它给了我很多编译错误(Converter类型不接受参数,不覆盖convert方法-convert()也存在很多错误).

is it possible to convert it automatically? I tried Converter inside spring bean but it gives me lot of compile errors (type Converter does not take parameters, does not override convert method - also lot of error for convert() as well).

@Bean
public ModelMapper studentModelMapper() {
....    
    Converter<String, LocalDate> toStringDate = new AbstractConverter<String, LocalDate>() {
        protected String convert(String source) {
            return source == null ? null : new LocalDate(source);
        }
    };
....
}

我对modelmapper不太熟悉.任何帮助是极大的赞赏.

I am not very familiar with modelmapper. Any help is greatly appreciated.

按照建议,我尝试使用DTO的LocalDate,但问题是当我在前端发送该实体(REST调用)时,我会遵循JSON.

As suggested I tried with LocalDate for DTO but the problem is when I send this entity at front (REST call) I get following JSON.

"dateOfBirth": {
   "year": 1972,
   "month": "JANUARY",
   "monthValue": 1,
   "dayOfMonth": 4,
   "dayOfWeek": "TUESDAY",
   "era": "CE",
   "dayOfYear": 4,
   "leapYear": true,
   "chronology": {
      "id": "ISO",
      "calendarType": "iso8601"
   }
}

我的前端开发人员需要"YYYY-MM-DD".

My front end developer need "YYYY-MM-DD".

推荐答案

如果要转换为LocalDate,则需要创建Provider,否则ModelMapper无法实例化LocalDate,因为它没有公共默认构造函数.

If you want to convert to LocalDate you need to create a Provider otherwise ModelMappercannot instantiate LocalDate because it doesn't have a public default constructor.

使用此配置,它将起作用:

Use this configuration and it will work:

 ModelMapper modelmapper = new ModelMapper();

    Provider<LocalDate> localDateProvider = new AbstractProvider<LocalDate>() {
        @Override
        public LocalDate get() {
            return LocalDate.now();
        }
    };

    Converter<String, LocalDate> toStringDate = new AbstractConverter<String, LocalDate>() {
        @Override
        protected LocalDate convert(String source) {
            DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate localDate = LocalDate.parse(source, format);
            return localDate;
        }
    };


    modelmapper.createTypeMap(String.class, LocalDate.class);
    modelmapper.addConverter(toStringDate);
    modelmapper.getTypeMap(String.class, LocalDate.class).setProvider(localDateProvider);

测试输出:

 String dateTest = "2000-09-27";
 LocalDate dateConverted = modelmapper.map(dateTest, LocalDate.class);

 System.out.println(dateConverted.toString()); //Output = 2000-09-27

这篇关于Modelmapper从String转换为LocalDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆