JavaのPOJOからいるProtobufにMapstruct(proto3)と地図一覧<文字列>

スムーズ:

私がいるProtobuf(proto3)にJavaからいくつかのPOJOをマッピングしようとしています。それらのいくつかのリストが含まれています。(例えば、リスト用)のPOJOとマッピングリストは問題ありませんが、私はUnsupportedOperationExceptionが取得しています。

List<Product>(これはcorrctly動作します):

ProductProtobuf.Builder map(Product product);

@Mapping(target = "productsList", source = "products")
ResponseProtobuf.Builder map(Response response);

List<String>(これは動作しません):

@Mapping(target = "usersList", source = "users")
ResponseProtobuf.Builder map(Response response);

また、私はビルダーのためのいくつかのマッパーを持っています:

public ResponseProtobuf.Builder responseBuilder() {
    return ResponseProtobuf.newBuilder();
}

public ProductProtobuf build(ProductProtobuf.Builder builder) {
    return builder.build();
}
フィリップ:

問題はMapStructが使用することですgetProductsList().addAll()あなたが使用する必要があり、これを避けるためにCollectionMappingStrategy.ADDER_PREFERRED collectionMappingStrategy見ていUserMapperからmapstruct-protobuf3 mapstruct例のレポでは。

一言で言えば、あなたが追加する必要があります。

@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED,
    nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)

あなたのマッパーへ。

あなたのビルダーに関する一つの情報は、場合にあなたが今MapStructにそのためのボックスのサポートが出ているとして、あなたがそれを必要としません1.3.0.Beta1を使用しています。

NB:あったバグでは正しく機能しませんでしたMapStructではProtocolStringListこのバグは1.3.0.Beta1で修正されています。あなたはこのバージョンでしようとした場合には、(場合には、あなたが動作するはずproductListですString

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=189847&siteId=1