[英]From string to enum using mapstruct
I want to convert String to enum using mapstruct 我想使用mapstruct将String转换为枚举
enum TestEnum {
NO("no");
String code;
TestEnum(String code) {
this.code = code
}
public String getCode() {
return code;
}
}
I have a code that I've got from service and I want to convert this code to Enum how to do it with easier way by mapstruct 我有一个从服务获得的代码,我想将此代码转换为Enum,如何通过mapstruct轻松实现
Here is a solution with an abstract mapper, but if you want you can convert it with a default methode or a class 这是一个使用抽象映射器的解决方案,但是如果需要,可以使用默认方法或类将其转换
@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;
}
}
The following code worked for me. 以下代码对我有用。
@Mappings({
@Mapping(source = "genderDTO.name", target = "genderName")
})
GenderRecord dtoTogenderRecord(GenderDTO genderDTO);
The result was: 结果是:
@Override
public GenderRecord dtoTogenderRecord(GenderDTO genderDTO) {
if ( genderDTO == null ) {
return null;
}
GenderRecord genderRecord = new GenderRecord();
if ( genderDTO.getName() != null ) {
genderRecord.setGenderName( Enum.valueOf( GenderType.class, genderDTO.getName() ) );
}
return genderRecord;
}
I also use the following at the interface level to ensure null checks: 我还在接口级别使用以下命令来确保进行空检查:
@Mapper(nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
MapStruct only calls the "setProperty(PropertyType)" function, so simply use polymorphism in your setters MapStruct只调用“ setProperty(PropertyType)”函数,因此只需在设置器中使用多态
For DTO: 对于DTO:
public void setStatus(String status) {
this.status = status;
}
public void setStatus(ProjectStatus status) {
this.status = status.toString();
}
For Entity: 对于实体:
public void setStatus(ProjectStatus status) {
this.status = status;
}
public void setStatus(String status) {
switch (status) {
case "PENDING_WITH_RM":
this.status = ProjectStatus.PENDING;
break;
case "PENDING_WITH_OPS":
this.status = ProjectStatus.PENDING;
break;
case "COMPLETED":
this.status = ProjectStatus.COMPLETED;
break;
default:
this.status = ProjectStatus.DRAFT;
}
}
In many cases there is a conversion between code and enum identifier possible: 在许多情况下,可能会在代码和枚举标识符之间进行转换:
enum TestEnum {
NO("no"),
_42("42"),
MAYBE_YES("maybe yes");
public final String code;
public static TestEnum fromCode(String c) {
c = c.toUpperCase().replace(' ', '_'); // Space to underscore.
if (c.matches("\\d.*")) {
c = "_" + c; // Starting digit prefixed by underscore.
}
return TestEnum.valueOf(c);
}
TestEnum(String code) {
this.code = code
}
public String getCode() {
return code;
}
}
The field code
no longer is necessary. 不再需要域
code
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.