Dozer 字符串到枚举映射
我有这样的枚举:
public enum PartnershipIndicator {
VENDOR("VENDOR"), COPARTNER("COPARTNER"), BUYER("BUYER");
String code;
private PartnershipIndicator(String code) {
this.code = code;
}
public String getCode() {
return code;
}
public static PartnershipIndicator valueOfCode(String code) {
for (PartnershipIndicator status : values()) {
if (status.getCode().equals(code)) {
return status;
}
}
throw new IllegalArgumentException(
"Partnership status cannot be resolved for code " + code);
}
@Override
public String toString() {
return code;
}
}
我需要将其转换为字符串,反之亦然。 现在,它是由自定义转换器完成的。但我想通过推土机映射来完成(如果可能的话)。如果我不向推土机配置写入任何映射,则会出现
org.dozer.MappingException: java.lang.NoSuchMethodException: by.dev.madhead.demo.test_java.model.PartnershipIndicator.<init>()
异常。我无法将默认公共构造函数添加到枚举中,因为这是不可能的。因此,我用内部代码和 valueOfCode() / toString() 编写了一个技巧。它不起作用。然后,我将其映射到推土机配置中:
<mapping>
<class-a>java.lang.String</class-a>
<class-b create-method="valueOfCode">by.dev.madhead.demo.test_java.model.PartnershipIndicator</class-b>
</mapping>
它不起作用。我尝试了 valueOfCode(),单向映射。什么都不起作用。枚举到字符串的转换也不起作用,我得到空字符串。 有什么想法吗?
I have such enum:
public enum PartnershipIndicator {
VENDOR("VENDOR"), COPARTNER("COPARTNER"), BUYER("BUYER");
String code;
private PartnershipIndicator(String code) {
this.code = code;
}
public String getCode() {
return code;
}
public static PartnershipIndicator valueOfCode(String code) {
for (PartnershipIndicator status : values()) {
if (status.getCode().equals(code)) {
return status;
}
}
throw new IllegalArgumentException(
"Partnership status cannot be resolved for code " + code);
}
@Override
public String toString() {
return code;
}
}
I need to convert it to String and vice versa. Now, it is done by custom converter. But i want to do it via dozer mappings (if it is possible). If i do not write any mappings to the dozer confing, i get
org.dozer.MappingException: java.lang.NoSuchMethodException: by.dev.madhead.demo.test_java.model.PartnershipIndicator.<init>()
exception. I cannot add default public constructor to enum, as it is not possible. So, i wrote a trick with internal code and valueOfCode() / toString(). It does not work. Then, i've mapped it in dozer config:
<mapping>
<class-a>java.lang.String</class-a>
<class-b create-method="valueOfCode">by.dev.madhead.demo.test_java.model.PartnershipIndicator</class-b>
</mapping>
It does not work. I tried valueOfCode(), one-way mappings. Nothing works. Enum to String conversion does not work too, i get empty Strings.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定这是否仍然是一个问题,但也许对任何搜索的人都有帮助。但这里是对此的实现解决方案:
构建异常消息时的 StrBuilder 类来自 apaches common-lang 库。但除此之外,还可以通过简单的反思来解决这个问题。只需添加到实现 CustomConverter 的类,然后在您的 dozer 映射 xml 文件中添加以下配置:
请注意,您只能在所有映射文件(如果有多个)之间列出一次配置,否则 dozer 会抱怨。为了简单起见,我通常所做的是将自定义转换器配置放在一个文件中。希望这有帮助!
Not sure if this is still an issue, but maybe help for anyone searching. But here is implemented solution to this:
The StrBuilder class when building the exception message is from the apaches common-lang libs. But other than that a simple reflection to solve this issue. Just add to a class that implements CustomConverter and then in your dozer mapping xml file add the following configuration:
Note that you can only list a configuration once between all of your mapping files (if you have multiple) otherwise dozer will complain. What I typically do is place my custom converter configurations in one file for simplicity. Hope this helps!
Dozer 中没有默认的字符串映射枚举。请参阅 Dozer 文档中的数据类型转换。因此,您有两个选择:
There isn't a default enum to String mapping in Dozer. See Data type conversion from Dozer docs. So you have two options: