故事背景
之前尝试过用 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/POM/4.0.0" xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache/POM/4.0.0 https://maven.apache/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.dtoan.OrganDTO;
import com.luxsun.weink.domain.poan.OrganPO;
import com.luxsun.weink.domain.voan.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 = organMapperanVoToDto(organVo);
organService.insertOrgan(organDto);
return ResultVoUtil.buildSuccess();
}
}