java - Spring Boot 看不到我的 Mapstruct 映射器
问题描述
我定义了映射器,但 Spring Boot 无法检测到它。几天没找到问题。请帮忙。在 IDEA 和 Netbeans 上试过。试图从此线程的主类上添加一些注释。
描述:
com.example.springmysqlelastic.service.impl.UserService 中构造函数的参数 1 需要找不到类型为“com.example.springmysqlelastic.mapper.UserMapper”的 bean。
行动:
考虑在你的配置中定义一个 'com.example.springmysqlelastic.mapper.UserMapper' 类型的 bean。
用户映射器.java
package com.example.springmysqlelastic.mapper;
import com.example.springmysqlelastic.model.Food;
import com.example.springmysqlelastic.model.FoodModel;
import com.example.springmysqlelastic.model.User;
import com.example.springmysqlelastic.model.UserModel;
import com.example.springmysqlelastic.model.dto.FoodDTO;
import com.example.springmysqlelastic.model.dto.UserDTO;
import org.mapstruct.Mapper;
import org.springframework.stereotype.Component;
import java.util.List;
//@Component
@Mapper(componentModel = "spring")
public interface UserMapper {
UserDTO toUserDTO(User user);
List<UserDTO> toUserDtos(List<User> users);
User toUser(UserDTO userDTO);
List<User> toUsers(List<UserDTO> userDTOS);
UserModel toUserModel(User user);
/*
FoodDTO toFoodDTO(Food food);
List<FoodDTO> toFoodDtos(List<Food> foods);
Food toFood(FoodDTO foodDTO);
List<Food> toFoods(List<FoodDTO> foodDTOS);
FoodModel toFoodModel(Food food);*/
}
用户服务
package com.example.springmysqlelastic.service.impl;
import com.example.springmysqlelastic.mapper.UserMapper;
import com.example.springmysqlelastic.model.User;
import com.example.springmysqlelastic.model.dto.UserDTO;
import com.example.springmysqlelastic.repo.IUserDAO;
import com.example.springmysqlelastic.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService implements IUserService {
private IUserDAO userDAO;
private UserMapper userMapper;
@Autowired
public UserService(IUserDAO userDAO, UserMapper userMapper) {
this.userDAO = userDAO;
this.userMapper = userMapper;
}
@Override
public UserDTO save(UserDTO userDTO) {
User user = this.userDAO.save(this.userMapper.toUser(userDTO));
return this.userMapper.toUserDTO(user);
}
@Override
public UserDTO findById(Long id) {
return this.userMapper.toUserDTO(this.userDAO.findById(id).orElse(null));
}
@Override
public List<UserDTO> findAll() {
return this.userMapper.toUserDtos(this.userDAO.findAll());
}
}
用户控制器.java
package com.example.springmysqlelastic.rest;
import com.example.springmysqlelastic.model.dto.UserDTO;
import com.example.springmysqlelastic.service.IUserService;
import com.example.springmysqlelastic.utils.PathResources;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user") //PathResources.USER
public class UserController {
private final IUserService userService;
@Autowired
public UserController(IUserService userService) {
this.userService = userService;
}
@PostMapping("/save") //PathResources.SAVE
public ResponseEntity<UserDTO> saveUser(@RequestBody UserDTO userDTO) {
return new ResponseEntity<>(this.userService.save(userDTO), HttpStatus.OK);
}
@GetMapping("/find-one/{id}") //PathResources.FIND_ONE + "/{" + PathResources.ID + "}"
public ResponseEntity<UserDTO> findById(@PathVariable Long id) {
return new ResponseEntity<>(this.userService.findById(id), HttpStatus.OK);
}
@GetMapping("/find-all") //PathResources.FIND_ALL
public ResponseEntity<List<UserDTO>> findById() {
return new ResponseEntity<>(this.userService.findAll(), HttpStatus.OK);
}
}
pom.xml
<?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.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-mysql-elastic</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring-mysql-elastic</name>
<description>Demo project for Mysql and ElasticSearch Synchronization in Spring</description>
<properties>
<java.version>1.8</java.version>
<mapstruct.version>1.4.0.Beta3</mapstruct.version>
<org.json.version>20190722</org.json.version>
<swagger.version>2.9.2</swagger.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${org.json.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<!--<version>1.18.12</version>-->
<type>jar</type>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>2.0.2</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>com.example.springmysqlelastic.SpringMysqlElasticApplication</mainClass>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
主班
package com.example.springmysqlelastic;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
//@SpringBootApplication(scanBasePackages={"com.example.springmysqlelastic"})
@EnableElasticsearchRepositories("com.example.springmysqlelastic.repo.elastic")
@EnableScheduling
//@ComponentScan(scanBasePackages = {"com.example.springmysqlelastic"})
//@EntityScan("com.example.springmysqlelastic.model")
//@EnableJpaRepositories("com.example.springmysqlelastic")
//@ComponentScan(basePackages = {"com.example.springmysqlelastic"})
//@EnableAutoConfiguration
public class SpringMysqlElasticApplication {
public static void main(String[] args) {
SpringApplication.run(SpringMysqlElasticApplication.class, args);
}
}
解决方案
在文档中说除了主要依赖外,还需要添加annotationProcessor
Gradle 示例:
dependencies {
implementation 'org.mapstruct:mapstruct:1.4.2.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final'
}
推荐阅读
- flutter - Flutter 中的通用 CRUD Http 函数
- javascript - angualr 10 中的 iframe-resizer 实现(使用 loader.js 动态添加 iframe)
- javascript - 在没有 fs 的情况下保存到 JSON?
- angular - Angular mat-form-field 在标签之间添加空格
- python - 从 OrderedDict 中移除 {}
- libcurl - 如何将 CURLU_ALLOW_SPACE 与 curl_easy_perform 一起使用
- c# - 使用全球化更改为剑道网格创建按钮文本
- angular - 无法读取未定义的属性(读取“已检查”)
- git - 错误:错误的 DVC 文件名 'my_server\models\*.tar.gz.dvc' 被 git 忽略
- visual-studio-code - 类文档自动生成工具