Dozer字符串到枚举映射 [英] Dozer String to enum mapping
问题描述
public enum PartnershipIndicator {
VENDOR(VENDOR),COPARTNER(哥斯达黎加),买家(买家);
字符串代码;
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代码)){
return status;
}
}
抛出新的IllegalArgumentException(
合作伙伴状态无法解码代码+代码);
}
@Override
public String toString(){
return code;
}
}
我需要将其转换为String,反之亦然。现在,这是通过自定义转换器完成的。但是我想通过dozer映射(如果可能的话)来实现。如果我不写任何映射到推土机confing,我得到
org.dozer.MappingException:java.lang.NoSuchMethodException: by.dev.madhead.demo.test_java.model.PartnershipIndicator。< init>()
exception 。我不能添加默认公共构造函数到枚举,因为这是不可能的。所以,我写了一个内部代码和valueOfCode()/ toString()的技巧。这是行不通的。然后,我将其映射到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>
它不工作。我尝试了valueOfCode(),单向映射。没有什么工作。枚举到字符串转换不起作用,我得到空字符串。
任何想法?
不知道这是否仍然是一个问题,但可能有助于任何人搜索。但是这里是实现的解决方案:
@Override
public Object convert(Object destination,Object source,Class& ?> destinationClass,Class<?> sourceClass){
if(source == null)
return null;
if(destinationClass!= null){
if(destinationClass.getSimpleName()。equalsIgnoreCase(String)){
return this.getString(source);
} else if(destinationClass.isEnum()){
return this.getEnum(destinationClass,source);
} else {
throw new MappingException(new StrBuilder(Converter).append(this.getClass()。getSimpleName())
.append(参数是:)
.append(destinationClass.getClass()。getName())
.append(and)
.append(source).toString());
}
}
返回null;
}
private Object getString(Object object){
String value = object.toString();
返回值;
}
private Object getEnum(Class<?> destinationClass,Object source){
Object enumeration = null;
方法[] ms = destinationClass.getMethods();
(方法m:ms){
if(m.getName()。equalsIgnoreCase(valueOf)){
try {
enumeration = m.invoke(destinationClass。 getClass(),(String)source);
}
catch(IllegalArgumentException e){
e.printStackTrace();
}
catch(IllegalAccessException e){
e.printStackTrace();
}
catch(InvocationTargetException e){
e.printStackTrace();
}
返回枚举;
}
}
返回null;
}
构建异常消息时的StrBuilder类是从apaches common-lang libs 。但除此之外,一个简单的反思来解决这个问题。只需添加一个实现CustomConverter的类,然后在你的dozer映射xml文件中添加以下配置:
< configuration>
< custom-converters>
< converter type =com.yourcompany.manager.utils.dozer.converters.EnumStringBiDirectionalDozerConverter>
< class-a> java.lang.Enum< / class-a>
< class-b> java.lang.String< / class-b>
< / converter>
< / custom-converters>
< / configuration>
请注意,您只能在所有映射文件之间列出配置(如果有多个映射文件)否则推土机会抱怨。我通常做的是将我的自定义转换器配置放在一个文件中以简化。希望这有帮助!
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?
Not sure if this is still an issue, but maybe help for anyone searching. But here is implemented solution to this:
@Override
public Object convert(Object destination, Object source, Class<?> destinationClass, Class<?> sourceClass) {
if(source == null)
return null;
if(destinationClass != null){
if(destinationClass.getSimpleName().equalsIgnoreCase("String")){
return this.getString(source);
}else if( destinationClass.isEnum()){
return this.getEnum(destinationClass, source);
}else{
throw new MappingException(new StrBuilder("Converter ").append(this.getClass().getSimpleName())
.append(" was used incorrectly. Arguments were: ")
.append(destinationClass.getClass().getName())
.append(" and ")
.append(source).toString());
}
}
return null;
}
private Object getString(Object object){
String value = object.toString();
return value;
}
private Object getEnum(Class<?> destinationClass, Object source){
Object enumeration = null;
Method [] ms = destinationClass.getMethods();
for(Method m : ms){
if(m.getName().equalsIgnoreCase("valueOf")){
try {
enumeration = m.invoke( destinationClass.getClass(), (String)source);
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
return enumeration;
}
}
return null;
}
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:
<configuration>
<custom-converters>
<converter type="com.yourcompany.manager.utils.dozer.converters.EnumStringBiDirectionalDozerConverter">
<class-a>java.lang.Enum</class-a>
<class-b>java.lang.String</class-b>
</converter>
</custom-converters>
</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字符串到枚举映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!