Mapstruct mapping - String to List<String>

user3611772

I am struggling to map a string object from source(Relation.class) and to a List of target(RelationListDTO.class) .

Relation.java

   public class Relation {

    private String name;
    private String email;
    private String completeAddress;

    // getters and setters
}

RelationListDTO.java

public class RelationListDTO {
        private String name;
        private String email;
        private List<Address> address;

        // getters and setters
    }

Address.java

public class Address{
private String street;
private String city;
// getters and setters
}

Mapper class

@Mapper

public interface RelationMapper {

    @Mapping(source = "completeAddress", target = "address.get(0).city")
            RelationListDTO relationToListDto(Relation relation);
} 

But it is not working. Could anyone please help.

Naveen

What you are trying to do using MapStruct is not possible. Because MapStruct doesn't work with run time objects. MapStruct only generated plain java code for mapping two beans. And I find your requirement is little unique. You have a list of Addresses but want to map only city from source object? You can still do like this

@Mapping( target = "address", source = "completeAddress")
RelationListDTO relationToListDto(Relation relation);

// MapStruct will know to use this method to map between a `String` and `List<Address>`
default List<Address> mapAddress(String relation){
      //create new arraylist
      // create new AddressObject and set completeAddress to address.city
     // add that to list and return list

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Mapstruct mapping Boolean to String

mapstruct - List<String> to List<Object>

Mapstruct: Mapping of "dirty" string fields to double

Mapping 2 String fields to OffsetDateTime with MapStruct

Mapstruct - Mapping customModel to it's string representation

Automapper mapping string to List<string>

Dart: mapping a list of string into a string

Mapping a string into a list of pairs

How mapping to List<> with mapstruct

Mapping Map<String, List<>> with List<>

List of String Entity Framework Mapping

JPA mapping for a List or Set<String>

Fluent Nhibernate List<string> mapping

Nested list mapping using MapStruct

MapStruct: filter a list before mapping

iBatis mapping: map a string field into a List<String>

Jackson Mapping List of String or simple String

mapstruct can not map property list of model to list of String

MapStruct List<String> to Single Object with Nested List of Objects

scala/spark mapping [String,List[String]] to String pairs

Mapping a list of type string to a list of objects with a string property using automapper

Mapping String with delimiters to list of strings or to new object

Mapping a Map<String,List<Foo>> in Hibernate

Mapping json string to a list of custom objects

Mapping element of a list of different type with mapstruct

MapStruct: mapping from object with a list of complex object

mapping Embedded List from single element with mapstruct

Mapping string to List<string> and vice versa using Automapper

Generate a list holding/making mapping between a List<string> and a List<int>