无法从多个父对象传递值以通过mapstruct进行映射

问题描述 投票:0回答:1

我正在映射一个需要在子级别映射父类的对象的场景。现在,我无法通过映射方法传递该对象参数。

Mapper class:

@Mapping(target = "segment", source = "flightSegment")
@Mapping(target = "PTC", source = "passengerFlight")
Itinerary mapProductAirToItinerary(ProductAir productAir);

List<PTC> mapPassengerFlightToPTC(List<PassengerFlight> passengerFlight);

@Mapping( target = "code", source = "passengerTypeCode")
@Mapping( target = "numberOfPassengers", source = "passengerQuantity")
@Mapping( target = "id", source = ***unknown***)
PTC createSOAPTCfromODflightProduct(PassengerFlight passengerFlight);

在上面的映射中,ProductAir具有一个名为ID的字符串对象,该字符串对象需要在具有目标ID的createSOAPTCfromODflightProduct方法中进行映射。由于无法通过mapPassengerFlightToPTC和createSOAPTCfromODflightProduct映射方法将productAir.id的值传递给它,因此我一直对此保持未知。无论如何,我可以将值从父级别传递到子映射级别吗?产品空气的结构如下:

"@type": "ProductAir",
      "totalDuration": "PT3H54M",
      "id": "p0",
      "FlightSegment": [
        {
          "id": "s1",
          "sequence": 1,
          "Flight": {
            "@type": "Flight",
            "duration": "PT3H54M",
            "carrier": "AA",
            "number": "2793",
            "operatingCarrier": "AA",
            "operatingCarrierName": "American Airlines",
            "equipment": "738",
            "id": "f1",
            "Departure": {
              "@type": "Departure",
              "location": "DEN",
              "date": "2020-07-09",
              "time": "23:40:00"
            },
            "Arrival": {
              "@type": "Arrival",
              "location": "MIA",
              "date": "2020-07-10",
              "time": "05:34:00"
            }
          }
mapstruct
1个回答
0
投票

所以..您可以尝试以下方法:


@Mapping(target = "segment", source = "flightSegment")
@Mapping(target = "PTC", source = "passengerFlight")
Itinerary mapProductAirToItinerary(ProductAir productAir, @Context String id);

List<PTC> mapPassengerFlightToPTC(List<PassengerFlight> passengerFlight, @Context String id);

@Mapping( target = "code", source = "passengerTypeCode")
@Mapping( target = "numberOfPassengers", source = "passengerQuantity")
@Mapping( target = "id", ignore = true )
PTC createSOAPTCfromODflightProduct(PassengerFlight passengerFlight, @Context String id);

@AfterMapping
default void createSOAPTCfromODflightProduct(PassengerFlight passengerFlight, @MappintTarget PTC target, @Context String id) {
     target.setId( id );
}


© www.soinside.com 2019 - 2024. All rights reserved.