'How to convert a nested string type to enum type using Mapstruct
please how easy is it to map from a String to an enum using mapstruct. So, I have the below set up for an entity and a dto:
public class Parent {
private FirstChild firstChild;
}
public class FirstChild {
private SecondChild secondChildOptionOne;
private SecondChild secondChildOptionTwo;
}
public class SecondChild {
private String colorType;
}
public class ParentEntity {
private FirstChildEntity firstChild;
}
public class FirstChildEntity {
private SecondChildEntity secondChildOptionOne;
private SecondChildEntity secondChildOptionTwo;
}
public class SecondChildEntity {
private Color colorType;
}
public enum Color {
GREEN("male"),
PINK("female");
private String gender;
private Color(String gender) {
this.gender = gender;
}
}
mapstruct
void update(FirstChild modal, @MappingTarget FirstChildEntity entity);
so the SecondChild dto has a string variable of type colorType that needs to be converted to the SecondChildEntity entity Color enum equivalent.
I am not able to get the mapping correctly. How could one go about this?
I could just write all these manually but would prefer mapstruct handles this IF possible.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
