本文介绍了推土机:列表的字符串到日期字段级别映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个 DTO (全部为String数据类型)映射到 VO (包含String,int ,boolean,Date)

I want to map a DTO (all are of String Data Types) to VO (contains String,int,boolean,Date)

StudentDTO

private StudentDetailDTO student;

StudentDetailDTO

private String sid;
private String name;
private String createDt;
private String studentInd;
private List<FeeReceiptDTO> feeDetails;

FeeReceiptDTO

private String semisterNum;
private String feeAmount;
private String paidOn;

StudentDetailVO:

private int sid;
private String name;
private Date createDt;
private boolean studentInd;
private List<FeeReceiptVO> feeDetails;

FeeReceiptVO:

private int semisterNum;
private Double feeAmount;
private Date paidOn;

我正在使用 DOZZER 映射我的DTO到VO

I am using DOZZER to map my DTO to VO String-to-Date-DozzerMapping

DozzerMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd">

    <mapping date-format="MM/dd/yyyy HH:mm" map-null="true" map-empty-string="true" wildcard="true" type="one-way" >
        <class-a>com.college.student.dto.StudentDTO</class-a>
        <class-b>com.college.student.vo.StudentVO</class-b>
        <field>
            <a>student.sid</a>
            <b>sid</b>
        </field>
        <field>
            <a>student.name</a>
            <b>name</b>
        </field>
        <field>
            <a>student.createDt</a>
            <b>createDt</b>
        </field>
        <field>
            <a>student.studentInd</a>
            <b>studentInd</b>
        </field>
        <field>
            <a date-format="MM/dd/yyyy HH:mm">student.feeDetails</a>
            <b>feeDetails</b>
        </field>

    </mapping>
</mappings>

但是,费用中的日期,即 paidOn 无法从字符串格式化到日期。

But, the Date in the feeDetails i.e., paidOn is unable to format from String to Date.

我做错了吗?

我需要为这个 paidOn 字段写一个 CustomStringToDateConvertor

Do i need to write a CustomStringToDateConvertor just for this paidOn field?

错误日志

ERROR [org.dozer.MappingProcessor] (http-localhost-127.0.0.1-9090-1) Field mapping error -->
  MapId: null
  Type: null
  Source parent class: com.college.student.dto.StudentDTO
  Source field name: paidOn
  Source field type: class java.lang.String
  Source field value: 01/01/2015 01:01
  Dest parent class: com.college.student.vo.StudentVO
  Dest field name: paidOn
  Dest field type: java.util.Date: org.dozer.converters.ConversionException: Unable to determine time in millis of source object
    at org.dozer.converters.DateConverter.convert(DateConverter.java:81) [dozer-5.4.0.jar:]
    at org.dozer.converters.PrimitiveOrWrapperConverter.convert(PrimitiveOrWrapperConverter.java:70) [dozer-5.4.0.jar:]

我尝试调试,我发现这个日期的 dateFormat 是<$ c $在Dozzer的日期转换器中的c> null org.dozer.converters.DateConvertor

I tried to Debug and i found that dateFormat for this date is coming as null in the Date convertor of Dozzer org.dozer.converters.DateConvertor

我在字段级中定义了日期格式,但不影响

I defined the date-format at field-level but it is not effected for List of Objects.

还有其他方法吗?

推荐答案

As,Dozer不能转换 String-To-Date - 在字段级映射列表

As,Dozer is not able to convert String-To-Date - At Field Level Mapping for a List.

我已经为List定义了一个新的映射,即将其视为 Object

I had defined a new mapping for the List i.e., considering it as a Object

我更改了 DozerMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd">

<mapping date-format="MM/dd/yyyy HH:mm" map-null="true" map-empty-string="true" wildcard="true" type="one-way" >
        <class-a>com.college.student.dto.FeeReceiptDTO</class-a>
        <class-b>com.college.student.vo.FeeReceiptVO</class-b>
</mapping>

    <mapping date-format="MM/dd/yyyy HH:mm" map-null="true" map-empty-string="true" wildcard="true" type="one-way" >
        <class-a>com.college.student.dto.StudentDTO</class-a>
        <class-b>com.college.student.vo.StudentVO</class-b>
        <field>
            <a>student.sid</a>
            <b>sid</b>
        </field>
        <field>
            <a>student.name</a>
            <b>name</b>
        </field>
        <field>
            <a>student.createDt</a>
            <b>createDt</b>
        </field>
        <field>
            <a>student.studentInd</a>
            <b>studentInd</b>
        </field>
        <field>
            <a>student.feeDetails</a>
            <b>feeDetails</b>
        </field>

    </mapping>
</mappings>

这篇关于推土机:列表的字符串到日期字段级别映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 01:42