英文:
is there a way to extract Mapstruct mapping?
问题
public class PersonDemo {
public static void main(String[] args) {
final PersonMapper mapper = Mappers.getMapper(PersonMapper.class);
Person person = mapper
.toPerson(new PersonDto()
.setFirstName("John")
.setName("Doe"));
System.out.println(person);
System.out.println("Expected " + toPersonDesc("firstName") + " to be firstName");
System.out.println("Expected " + toPersonDesc("name") + " to be lastName");
}
@Mapper
public interface PersonMapper {
@Mapping(target = "lastName", source = "name")
Person toPerson(PersonDto dto);
}
//expected method. to be generated somehow from the mapping.
static String toPersonDesc(String fieldName) {
switch (fieldName) {
case "name": return "lastName";
case "firstName": return "firstName";
}
return null;
}
@Data
@Accessors(chain = true)
public static class Person {
private String lastName;
private String firstName;
}
@Data
@Accessors(chain = true)
public static class PersonDto {
private String name;
private String firstName;
}
}
英文:
Let says I have a basic case where I map an object to an other, I need to declare the mapping in the Mapper class.
For validation I may want to remember the original field name and his corresponding mapping.
Is there a way to do it with mapstruct without having to do a mapping "manually"
here is an example of what I would expect.
public class PersonDemo {
public static void main(String[] args) {
final PersonMapper mapper = Mappers.getMapper(PersonMapper.class);
Person person = mapper
.toPerson(new PersonDto()
.setFirstName("John")
.setName("Doe"));
System.out.println(person);
System.out.println("Expected " + toPersonDesc("firstName") + " to be firstName");
System.out.println("Expected " + toPersonDesc("name") + " to be lastName");
}
@Mapper
public interface PersonMapper {
@Mapping(target = "lastName", source = "name")
Person toPerson(PersonDto dto);
}
//expected method. to be generated somehow from the mapping.
static String toPersonDesc(String fieldName) {
switch (fieldName) {
case "name": return "lastName";
case "firstName": return "firstName";
}
return null;
}
@Data
@Accessors(chain = true)
public static class Person {
private String lastName;
private String firstName;
}
@Data
@Accessors(chain = true)
public static class PersonDto {
private String name;
private String firstName;
}
}
答案1
得分: 1
这在Mapstruct中是不可能的。有另一个类似于您的问题的问题:https://stackoverflow.com/q/62675889/4494577。那里的答案描述了解决问题的“某种”方法,但它非常有限 - 它仅从@Mapping
注释中提取信息(不支持隐式映射)。
尽管如此,如果能够实现这个功能将会非常有用 - 您应该向Mapstruct团队提出这个建议。
英文:
It's impossible with Mapstruct. There's another question similar to yours: https://stackoverflow.com/q/62675889/4494577. The answer there describes "some" approach to the problem, but it's very limited - it simply scrapes the information from the @Mapping
annotations (no support for implicit mappings).
It would be quite useful to have this though - you should propose this feature to the Mapstruct team.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论