首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >MapStruct Java Streams

MapStruct Java Streams
EN

Stack Overflow用户
提问于 2018-10-01 20:56:37
回答 2查看 1.9K关注 0票数 0

你好,我是一个非常新手的流等,只是为了培训的目的,我试图建立简单的api与mapstruct。我的问题是,我真的不知道如何返回转换的父值与转换的字段在这个实体中。

下面是我的简单ObjecDTO

public class RecipeDTO {


    private Integer id;

    private String title;

    private String description;

    private String imgUrl;

    private String directions;

    private String prepTime;

    private Integer servings;

    private String category;

    private Set<IngredientDTO> ingredientDTOSet = new HashSet<>();
}

实体

public class Recipe {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "recipe_id")
    private Integer id;

    @NotNull
    @Column(name = "recipe_title")
    private String title;

    @NotNull
    @Column(name = "recipe_description")
    private String description;

    @Column(name = "recipe_img_url")
    private String imgUrl;

    @NotNull
    @Column(name = "recipe_direction")
    private String directions;

    @NotNull
    @Column(name = "prep_time")
    private String prepTime;

    @NotNull
    @Column(name = "servings")
    private Integer servings;

    @NotNull
    @Column(name = "category")
    private String category;

    @OneToMany(mappedBy = "recipe",
            cascade = CascadeType.ALL)
    private Set<Ingredient> ingredients = new HashSet<>();

}

服务(只有一种方法)

@Override
    public List<RecipeDTO> findAll() {

        log.debug("\n" + this.getClass().getSimpleName() + " -> findAll() \n");

        return recipeRepository.findAll()
                .stream()
                .map(recipe -> RecipeMapper.INSTANCE.recipeToRecipeDTO(recipe))
                .collect(Collectors.toList());
    }


@Mapper(componentModel = "spring", uses = {IngredientMapper.class})
public interface RecipeMapper {

    RecipeMapper INSTANCE = Mappers.getMapper(RecipeMapper.class);

    @Mappings({
            @Mapping(source = "ingredients", target = "ingredientDTOSet")
    })
    RecipeDTO recipeToRecipeDTO(Recipe recipe);


}

如何转换嵌套bean?当我尝试获取所有处方时,抛出异常

classes: null at com.mateuszgeborski.recipeappapi.api.mapper.RecipeMapperImpl.ingredientSetToIngredientDTOSet(RecipeMapperImpl.java:52) ~classes/:na at com.mateuszgeborski.recipeappapi.api.mapper.RecipeMapperImpl.recipeToRecipeDTO(RecipeMapperImpl.java:32) ~classes/:na at com.mateuszgeborski.recipeappapi.service.RecipeServiceImpl.lambda$findAll$0(RecipeServiceImpl.java:38) ~classes/:na at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~na:1.8.0_181 at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382) ~na:1.8.0_181在etc的java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~na:1.8.0_181。

它看起来像是有成分,但当我使用食谱而不是DTO时,一切都很好。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-02 14:21:09

问题是你把componentModel = "spring"Mappers工厂混在一起了。当您使用spring组件模型时,所使用的映射器将在生成的类中自动连接,并且Mappers工厂仅初始化实例。这意味着您的配方映射器没有被注入。

为了解决这个问题,你应该在任何地方使用默认的componentModel,或者使用spring并注入你的映射器。

附注:你现在做映射的方式并不是很好。您只需添加一个方法来将列表映射到映射器并调用它,而不是使用流。

票数 0
EN

Stack Overflow用户

发布于 2018-10-02 03:36:03

这不是一个关于java stream的问题。Java stream api正在调用recipeToRecipeDTO方法,该方法正在失败。理想情况下,recipeToRecipeDTO方法应该能够将整个配方对象转换为RecipeDTO。

要解决这个问题,您需要在RecipeMapper中添加一个方法,该方法将一个Ingredient实体对象转换为IngredientDTO

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52591677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档