我有一个User和UserDTO类,但在dto类我不想用户LocalDateTime,我想将其转换为长类型。 (因为protobuf不支持日期)。 所以在代码中:
我的用户实体类:
public class User { private String name,password; private LocalDateTime date; //getters-setters, tostring.. }我的DTO:
public class UserDTO { private String name,password; private long date; //getters-setters, tostring.. }你会看到实体User中的日期是LocalDateTime,而DTO中的日期很长。 我想使用这个dozermapper:
UserDTO destObject = mapper.map(user, UserDTO.class);LocalDateTime将代码更改为long:
private static long setDateToLong(LocalDateTime date) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); String dateString = date.format(formatter); return Long.parseLong(dateString); }映射器是否可能知道将LocalDateTime更改为long? 我可以以某种方式配置它吗? 感谢帮助!
I have a User and UserDTO class but in dto class i dont want to user LocalDateTime, i want to convert it to long type. (because protobuf not support date). So in code:
My User entity class:
public class User { private String name,password; private LocalDateTime date; //getters-setters, tostring.. }My DTO:
public class UserDTO { private String name,password; private long date; //getters-setters, tostring.. }And you see that the date in entity User is LocalDateTime, and in DTO is long. I want to use this dozermapper:
UserDTO destObject = mapper.map(user, UserDTO.class);The LocalDateTime changing code to long:
private static long setDateToLong(LocalDateTime date) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); String dateString = date.format(formatter); return Long.parseLong(dateString); }Is it possible that the mapper knows that change the LocalDateTime to long? Can i somehow configure it? Thanks for the helps!
最满意答案
最后我发现了一个从LocalDateTime创建到String的溶剂,并从String返回到LocalDateTime。 我必须创建一个转换器:
public class DozerConverter implements CustomConverter { @Override public Object convert(Object destination, Object source, Class destClass, Class sourceClass) { DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; if(source instanceof String) { String sourceTime = (String) source; return LocalDateTime.parse(sourceTime, formatter); } else if (source instanceof LocalDateTime) { LocalDateTime sourceTime = (LocalDateTime) source; return sourceTime.toString(); } return null; }}
在我的自定义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> <class-a>mypackage.UserDTO</class-a> <class-b>mypackage.User</class-b> <field custom-converter="mypackage.DozerConverter"> <a>lastLoggedInTime</a> <b>lastLoggedInTime</b> </field> </mapping> </mappings>我认为它可以与任何数据类型一起工作,只需要编写更多的转换器,或者将此转换器编写为智能。
Finally i found a sollution which is create from LocalDateTime to String and back from String to LocalDateTime. I have to create a converter:
public class DozerConverter implements CustomConverter { @Override public Object convert(Object destination, Object source, Class destClass, Class sourceClass) { DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; if(source instanceof String) { String sourceTime = (String) source; return LocalDateTime.parse(sourceTime, formatter); } else if (source instanceof LocalDateTime) { LocalDateTime sourceTime = (LocalDateTime) source; return sourceTime.toString(); } return null; }}
And in my custom xml i have to add the custom-converter attribute like this one:
<?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> <class-a>mypackage.UserDTO</class-a> <class-b>mypackage.User</class-b> <field custom-converter="mypackage.DozerConverter"> <a>lastLoggedInTime</a> <b>lastLoggedInTime</b> </field> </mapping> </mappings>I think it can work with any data types, just have to write more converter, or write this converter smart.
更多推荐
LocalDateTime,long,date,UserDTO,class,电脑培训,计算机培训,IT培训"/> <meta na
发布评论