我想使用mapstruct将String转换为枚举
enum TestEnum {
NO("no");
String code;
TestEnum(String code) {
this.code = code
}
public String getCode() {
return code;
}
}
我有一个从服务获得的代码,我想将此代码转换为Enum,如何通过mapstruct轻松实现
这是一个使用抽象映射器的解决方案,但是如果需要,可以使用默认方法或类将其转换
@Mapper
public abstract class TestMapper {
abstract Source toSource(Target target);
abstract Target totarget(Source source);
String toString(TestEnum test){
return test.getCode();
}
TestEnum toEnum(String code){
for (TestEnum testEnum : TestEnum.values()) {
if(testEnum.equals(code)){
return testEnum;
}
}
return null;
}
}
public class Source {
String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
public class Target {
TestEnum value;
public TestEnum getValue() {
return value;
}
public void setValue(TestEnum value) {
this.value = value;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句