英文:
How to add space in expression in MapStruct
问题
I have a requirement to add firstName and lastName from source to fullName in target. I want first & last name to be separated by space. But I am unable to write the proper mapping for it.
My Source and target class-
public class Source {
private int id;
private String firstName;
private String lastName;
private List<String> addressList;
}
public class Target {
private int id;
private String fullName;
private String city;
}
Here is my mapper interface-
@Mapper
public interface SourceTargetMapper {
SourceTargetMapper INSTANCE = Mappers.getMapper(SourceTargetMapper.class);
@Mapping(expression = "java(source.getFirstName()+source.getLastName())", target = "fullName")
@Mapping(expression = "java(source.getAddressList().get(0))", target = "city")
Target sourceToTarget(Source source);
@InheritInverseConfiguration
Source targetToSource(Target target);
}
Adding space in middle is giving me error-
@Mapping(expression = "java(source.getFirstName()+" "+source.getLastName())", target = "fullName")
If there is any other solution or any approach for this?
英文:
I have a requirement to add firstName and lastName from source to fullName in target. I want first & last name to be separated by space. But I am unable to write the proper mapping for it.
My Source and target class-
public class Source {
private int id;
private String firstName;
private String lastName;
private List<String> addressList;
}
public class Target {
private int id;
private String fullName;
private String city;
}
Here is my mapper interface-
@Mapper
public interface SourceTargetMapper {
SourceTargetMapper INSTANCE = Mappers.getMapper(SourceTargetMapper.class);
@Mapping(expression = "java(source.getFirstName()+source.getLastName())", target = "fullName")
@Mapping(expression = "java(source.getAddressList().get(0))", target = "city")
Target sourceToTarget(Source source);
@InheritInverseConfiguration
Source targetToSource(Target target);
}
Adding space in middle is giving me error-
@Mapping(expression = "java(source.getFirstName()+" "+source.getLastName())", target = "fullName")
If there is any other solution or any approach for this ?
答案1
得分: 3
你需要转义引号 "
。
例如:
@Mapping(expression = "java(source.getFirstName()+ \" \" +source.getLastName())", target = "fullName")
英文:
You will need to escape the quote "
.
e.g.
@Mapping(expression = "java(source.getFirstName()+ \" \" +source.getLastName())", target = "fullName")
答案2
得分: 0
我发现了另一种使用装饰器模式来编写映射的方法,就像在 Map Struct 文档中提到的那样。
我将只复制粘贴链接和示例代码在这里。
链接:https://mapstruct.org/documentation/dev/reference/html/#customizing-mappers-using-decorators
以下是来自 mapstruct 文档的代码示例 -
@Mapper
@DecoratedWith(PersonMapperDecorator.class)
public interface PersonMapper {
PersonMapper INSTANCE = Mappers.getMapper( PersonMapper.class );
PersonDto personToPersonDto(Person person);
AddressDto addressToAddressDto(Address address);
}
public abstract class PersonMapperDecorator implements PersonMapper {
private final PersonMapper delegate;
public PersonMapperDecorator(PersonMapper delegate) {
this.delegate = delegate;
}
@Override
public PersonDto personToPersonDto(Person person) {
PersonDto dto = delegate.personToPersonDto( person );
dto.setFullName( person.getFirstName() + " " + person.getLastName() );
return dto;
}
}
英文:
I found another way to write the Mapping by using decorator pattern as mentioned in the Map Struct documentation by using the decorator pattern.
I will just copy paste the link and example here.
https://mapstruct.org/documentation/dev/reference/html/#customizing-mappers-using-decorators
And here goes the code taken from mapstruct documentation -
@Mapper
@DecoratedWith(PersonMapperDecorator.class)
public interface PersonMapper {
PersonMapper INSTANCE = Mappers.getMapper( PersonMapper.class );
PersonDto personToPersonDto(Person person);
AddressDto addressToAddressDto(Address address);
}
public abstract class PersonMapperDecorator implements PersonMapper {
private final PersonMapper delegate;
public PersonMapperDecorator(PersonMapper delegate) {
this.delegate = delegate;
}
@Override
public PersonDto personToPersonDto(Person person) {
PersonDto dto = delegate.personToPersonDto( person );
dto.setFullName( person.getFirstName() + " " + person.getLastName() );
return dto;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论