MapStruct - Consider defining a bean of type in your configuration.

Posted 放羊的牧码

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MapStruct - Consider defining a bean of type in your configuration.相关的知识,希望对你有一定的参考价值。

故事背景

之前尝试过用 INSTANCE 方式来使用 MapStruct,但作为 Spring 玩家,还是习惯于使用 Bean 的方式来注入,于是乎发现注入失败!!!

报错信息

Consider defining a bean of type in your configuration.

解决方案

  • 其实还是 Maven 里 Lombok 和 MapStruct 兼容性问题引起的,如下附上正确配置方案
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.18.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>com.weink</groupId>
    <artifactId>weink-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>weink-server</name>
    <description>weink-server</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>1.4.1.Final</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>1.18.12</version>
                        </path>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>1.4.1.Final</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
package com.luxsun.weink.convert;

import com.luxsun.weink.domain.dto.organ.OrganDTO;
import com.luxsun.weink.domain.po.organ.OrganPO;
import com.luxsun.weink.domain.vo.organ.OrganVO;
import org.mapstruct.Mapper;

/**
 * @author Lux Sun
 * @date 2022/7/18
 */
@Mapper(componentModel = "spring")
public interface OrganMapper 

    OrganPO organDtoToPo(OrganDTO organDto);

    OrganDTO organVoToDto(OrganVO OrganVo);
/**
 * @author Lux Sun
 * @date 2022/7/8
 */
@Slf4j
@RestController
@RequestMapping("/organ")
public class OrganController 

    @Resource
    private OrganService organService;

    @Resource
    private OrganMapper organMapper;

    /**
     * 新增机构
     * @param organVo
     */
    @PostMapping
    public ResultVO<Object> insertOrgan(@RequestBody OrganVO organVo) 
        OrganDTO organDto = organMapper.organVoToDto(organVo);
        organService.insertOrgan(organDto);
        return ResultVoUtil.buildSuccess();
    

以上是关于MapStruct - Consider defining a bean of type in your configuration.的主要内容,如果未能解决你的问题,请参考以下文章

MapStruct 一文读懂

MapStruct 映射工具

MapStruct - 找不到实现

MapStruct类型之间映射的实现

MapStruct类型之间映射的实现

MapStruct类型之间映射的实现