如何在MapStruct中的表达式中添加空格

huangapple go评论68阅读模式
英文:

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 = &quot;java(source.getFirstName()+source.getLastName())&quot;, target = &quot;fullName&quot;)
@Mapping(expression = &quot;java(source.getAddressList().get(0))&quot;, target = &quot;city&quot;)
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&lt;String&gt; 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 = &quot;java(source.getFirstName()+source.getLastName())&quot;, target = &quot;fullName&quot;)
    @Mapping(expression = &quot;java(source.getAddressList().get(0))&quot;, target = &quot;city&quot;)
    Target sourceToTarget(Source source);

    @InheritInverseConfiguration
    Source targetToSource(Target target);
}

Adding space in middle is giving me error-

@Mapping(expression = &quot;java(source.getFirstName()+&quot; &quot;+source.getLastName())&quot;, target = &quot;fullName&quot;)

If there is any other solution or any approach for this ?

答案1

得分: 3

你需要转义引号 &quot;

例如:

@Mapping(expression = "java(source.getFirstName()+ \" \" +source.getLastName())", target = "fullName")
英文:

You will need to escape the quote &quot;.

e.g.

@Mapping(expression = &quot;java(source.getFirstName()+ \&quot; \&quot; +source.getLastName())&quot;, target = &quot;fullName&quot;)

答案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() + &quot; &quot; + person.getLastName() );
        return dto;
    }
}

huangapple
  • 本文由 发表于 2020年8月13日 19:49:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63394541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定