Spring Boot无法识别我的Mapstruct映射器。

huangapple go评论80阅读模式
英文:

Spring Boot Can't See My Mapstruct Mapper

问题

I defined the mapper but Spring Boot can't detect it. I couldn't find the problem for days. Please help. Tried on IDEA and Netbeans. Tried to add some annotations on main class from this thread.

> Description:
>
> Parameter 1 of constructor in
> com.example.springmysqlelastic.service.impl.UserService required a
> bean of type 'com.example.springmysqlelastic.mapper.UserMapper' that
> could not be found.
>
>
> Action:
>
> Consider defining a bean of type
> 'com.example.springmysqlelastic.mapper.UserMapper' in your
> configuration.

UserMapper.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);*/
}

UserService.java

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());
    }
}

UserController.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>
                <

<details>
<summary>英文:</summary>

I defined the mapper but Spring Boot can&#39;t detect it. I couldn&#39;t find the problem for days. Please help. Tried on IDEA and Netbeans. Tried to add some annotations on main class from [this thread][1].

&gt; *Description:*
&gt; 
&gt; Parameter 1 of constructor in
&gt; com.example.springmysqlelastic.service.impl.UserService required a
&gt; bean of type &#39;com.example.springmysqlelastic.mapper.UserMapper&#39; that
&gt; could not be found.
&gt; 
&gt; 
&gt; *Action:*
&gt; 
&gt; **Consider defining a bean of type
&gt; &#39;com.example.springmysqlelastic.mapper.UserMapper&#39; in your
&gt; configuration.**

**UserMapper.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 = &quot;spring&quot;)
    public interface UserMapper {
    
        UserDTO toUserDTO(User user);
    
        List&lt;UserDTO&gt; toUserDtos(List&lt;User&gt; users);
    
        User toUser(UserDTO userDTO);
    
        List&lt;User&gt; toUsers(List&lt;UserDTO&gt; userDTOS);
    
        UserModel toUserModel(User user);
    /*
        FoodDTO toFoodDTO(Food food);
    
        List&lt;FoodDTO&gt; toFoodDtos(List&lt;Food&gt; foods);
    
        Food toFood(FoodDTO foodDTO);
    
        List&lt;Food&gt; toFoods(List&lt;FoodDTO&gt; foodDTOS);
    
        FoodModel toFoodModel(Food food);*/
    }

**UserService**

    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&lt;UserDTO&gt; findAll() {
            return this.userMapper.toUserDtos(this.userDAO.findAll());
        }
    }

**UserController.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(&quot;/user&quot;) //PathResources.USER
    public class UserController {
    
        private final IUserService userService;
    
        @Autowired
        public UserController(IUserService userService) {
            this.userService = userService;
        }
    
        @PostMapping(&quot;/save&quot;) //PathResources.SAVE
        public ResponseEntity&lt;UserDTO&gt; saveUser(@RequestBody UserDTO userDTO) {
            return new ResponseEntity&lt;&gt;(this.userService.save(userDTO), HttpStatus.OK);
        }
    
        @GetMapping(&quot;/find-one/{id}&quot;) //PathResources.FIND_ONE + &quot;/{&quot; + PathResources.ID + &quot;}&quot;
        public ResponseEntity&lt;UserDTO&gt; findById(@PathVariable Long id) {
            return new ResponseEntity&lt;&gt;(this.userService.findById(id), HttpStatus.OK);
        }
    
        @GetMapping(&quot;/find-all&quot;) //PathResources.FIND_ALL
        public ResponseEntity&lt;List&lt;UserDTO&gt;&gt; findById() {
            return new ResponseEntity&lt;&gt;(this.userService.findAll(), HttpStatus.OK);
        }
    }

**pom.xml**

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
    &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
             xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
        &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
        &lt;parent&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
            &lt;version&gt;2.3.1.RELEASE&lt;/version&gt;
            &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
        &lt;/parent&gt;
        &lt;groupId&gt;com.example&lt;/groupId&gt;
        &lt;artifactId&gt;spring-mysql-elastic&lt;/artifactId&gt;
        &lt;packaging&gt;jar&lt;/packaging&gt;
        &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
        &lt;name&gt;spring-mysql-elastic&lt;/name&gt;
        &lt;description&gt;Demo project for Mysql and ElasticSearch Synchronization in Spring&lt;/description&gt;
    
        &lt;properties&gt;
            &lt;java.version&gt;1.8&lt;/java.version&gt;
            &lt;mapstruct.version&gt;1.4.0.Beta3&lt;/mapstruct.version&gt;
            &lt;org.json.version&gt;20190722&lt;/org.json.version&gt;
            &lt;swagger.version&gt;2.9.2&lt;/swagger.version&gt;
        &lt;/properties&gt;
    
        &lt;dependencies&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
            &lt;/dependency&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
            &lt;/dependency&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
            &lt;/dependency&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                &lt;artifactId&gt;spring-boot-starter-data-elasticsearch&lt;/artifactId&gt;
            &lt;/dependency&gt;
                    &lt;dependency&gt;
                &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
            &lt;/dependency&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;mysql&lt;/groupId&gt;
                &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
            &lt;/dependency&gt;
    
            &lt;dependency&gt;
                &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
                &lt;scope&gt;test&lt;/scope&gt;
                &lt;exclusions&gt;
                    &lt;exclusion&gt;
                        &lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
                        &lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
                    &lt;/exclusion&gt;
                &lt;/exclusions&gt;
            &lt;/dependency&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
                &lt;artifactId&gt;mapstruct-jdk8&lt;/artifactId&gt;
                &lt;version&gt;${mapstruct.version}&lt;/version&gt;
            &lt;/dependency&gt;
    
            &lt;!-- https://mvnrepository.com/artifact/org.json/json --&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;org.json&lt;/groupId&gt;
                &lt;artifactId&gt;json&lt;/artifactId&gt;
                &lt;version&gt;${org.json.version}&lt;/version&gt;
            &lt;/dependency&gt;
    &lt;!-- https://mvnrepository.com/artifact/com.alibaba/fastjson 
    &lt;dependency&gt;
        &lt;groupId&gt;com.alibaba&lt;/groupId&gt;
        &lt;artifactId&gt;fastjson&lt;/artifactId&gt;
        &lt;version&gt;1.2.73&lt;/version&gt;
    &lt;/dependency&gt;--&gt;
    
            &lt;dependency&gt;
                &lt;groupId&gt;io.springfox&lt;/groupId&gt;
                &lt;artifactId&gt;springfox-swagger2&lt;/artifactId&gt;
                &lt;version&gt;${swagger.version}&lt;/version&gt;
            &lt;/dependency&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;io.springfox&lt;/groupId&gt;
                &lt;artifactId&gt;springfox-swagger-ui&lt;/artifactId&gt;
                &lt;version&gt;${swagger.version}&lt;/version&gt;
            &lt;/dependency&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
                &lt;artifactId&gt;lombok&lt;/artifactId&gt;
                &lt;!--&lt;version&gt;1.18.12&lt;/version&gt;--&gt;
                &lt;type&gt;jar&lt;/type&gt;
            &lt;/dependency&gt;
    &lt;!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;
        &lt;artifactId&gt;gson&lt;/artifactId&gt;
        &lt;version&gt;2.8.6&lt;/version&gt;
    &lt;/dependency&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;jakarta.validation&lt;/groupId&gt;
                &lt;artifactId&gt;jakarta.validation-api&lt;/artifactId&gt;
                &lt;version&gt;2.0.2&lt;/version&gt;
                &lt;type&gt;jar&lt;/type&gt;
            &lt;/dependency&gt;
        &lt;/dependencies&gt;
    
        &lt;build&gt;
            &lt;plugins&gt;
                &lt;plugin&gt;
                    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                    &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
                    &lt;executions&gt;
                        &lt;execution&gt;
                            &lt;goals&gt;
                                &lt;goal&gt;repackage&lt;/goal&gt;
                            &lt;/goals&gt;
                            &lt;configuration&gt;
                                &lt;mainClass&gt;com.example.springmysqlelastic.SpringMysqlElasticApplication&lt;/mainClass&gt;
                            &lt;/configuration&gt;
                        &lt;/execution&gt;
                    &lt;/executions&gt;
                &lt;/plugin&gt;
                &lt;plugin&gt;
                    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                    &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                    &lt;configuration&gt;
                        &lt;source&gt;${java.version}&lt;/source&gt;
                        &lt;target&gt;${java.version}&lt;/target&gt;
                        &lt;annotationProcessorPaths&gt;
                            &lt;path&gt;
                                &lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
                                &lt;artifactId&gt;mapstruct-processor&lt;/artifactId&gt;
                                &lt;version&gt;${mapstruct.version}&lt;/version&gt;
                            &lt;/path&gt;
                        &lt;/annotationProcessorPaths&gt;
                    &lt;/configuration&gt;
                &lt;/plugin&gt;
            &lt;/plugins&gt;
        &lt;/build&gt;
    
    &lt;/project&gt;

**Main class**

    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={&quot;com.example.springmysqlelastic&quot;})
    @EnableElasticsearchRepositories(&quot;com.example.springmysqlelastic.repo.elastic&quot;)
    @EnableScheduling
    //@ComponentScan(scanBasePackages = {&quot;com.example.springmysqlelastic&quot;})
    //@EntityScan(&quot;com.example.springmysqlelastic.model&quot;)
    //@EnableJpaRepositories(&quot;com.example.springmysqlelastic&quot;)
    //@ComponentScan(basePackages = {&quot;com.example.springmysqlelastic&quot;})
    //@EnableAutoConfiguration
    
    public class SpringMysqlElasticApplication {
    
        public static void main(String[] args) {
    
            SpringApplication.run(SpringMysqlElasticApplication.class, args);
        }
    
    }



  [1]: https://stackoverflow.com/questions/40384056/consider-defining-a-bean-of-type-package-in-your-configuration-spring-boot

</details>


# 答案1
**得分**: 1

在[文档][1]中提到,除了主要的依赖项外,您还需要添加annotationProcessor。

Gradle示例:

```gradle
dependencies {
    implementation 'org.mapstruct:mapstruct:1.4.2.Final'
    annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final'
}
英文:

In the documentation says that in addition to the main dependency, you need to add annotationProcessor

Example for Gradle:

dependencies {
implementation &#39;org.mapstruct:mapstruct:1.4.2.Final&#39;
annotationProcessor &#39;org.mapstruct:mapstruct-processor:1.4.2.Final&#39;
}

答案2

得分: 0

我认为这是与Swagger与Mapstruct库冲突的问题。

我修改了您的POM文件如下所示,并且对我起作用。这里有一些关于它的文档:https://www.programmersought.com/article/6208308983/

<?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>
    <!-- 其他依赖 -->
    <!-- 修改Swagger依赖如下 -->
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>${swagger.version}</version>
      <exclusions>
        <exclusion>
          <groupId>org.mapstruct</groupId>
          <artifactId>mapstruct</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>${swagger.version}</version>
      <exclusions>
        <exclusion>
          <groupId>org.mapstruct</groupId>
          <artifactId>mapstruct</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <!-- 其他依赖 -->
  </dependencies>

  <!-- 其他配置 -->

</project>

您可以检查它是否适用于您。

更新
在代码生成步骤之后,它生成了以下内容:

package com.example.springmysqlelastic.mapper;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import org.springframework.stereotype.Component;

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2020-08-11T16:40:57+0530",
    comments = "version: 1.4.0.Beta3, compiler: javac, environment: Java 1.8.0_265 (Private Build)"
)
@Component
public class UserMapperImpl implements UserMapper {

    @Override
    public UserDTO toUserDTO(User user) {
        if ( user == null ) {
            return null;
        }

        UserDTO userDTO = new UserDTO();

        return userDTO;
    }

    @Override
    public List<UserDTO> toUserDtos(List<User> users) {
        if ( users == null ) {
            return null;
        }

        List<UserDTO> list = new ArrayList<UserDTO>( users.size() );
        for ( User user : users ) {
            list.add( toUserDTO( user ) );
        }

        return list;
    }

    @Override
    public User toUser(UserDTO userDTO) {
        if ( userDTO == null ) {
            return null;
        }

        User user = new User();

        return user;
    }

    @Override
    public List<User> toUsers(List<UserDTO> userDTOS) {
        if ( userDTOS == null ) {
            return null;
        }

        List<User> list = new ArrayList<User>( userDTOS.size() );
        for ( UserDTO userDTO : userDTOS ) {
            list.add( toUser( userDTO ) );
        }

        return list;
    }
}

希望这对您有所帮助。

英文:

I think its an issue with swagger conflicting with the mapstruct library..

I modified your POM like below and it started working for me. They have some documentation here that talks about it. https://www.programmersought.com/article/6208308983/

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;2.3.1.RELEASE&lt;/version&gt;
&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;groupId&gt;com.example&lt;/groupId&gt;
&lt;artifactId&gt;spring-mysql-elastic&lt;/artifactId&gt;
&lt;packaging&gt;jar&lt;/packaging&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;spring-mysql-elastic&lt;/name&gt;
&lt;description&gt;Demo project for Mysql and ElasticSearch Synchronization in Spring&lt;/description&gt;
&lt;properties&gt;
&lt;java.version&gt;1.8&lt;/java.version&gt;
&lt;mapstruct.version&gt;1.4.0.Beta3&lt;/mapstruct.version&gt;
&lt;org.json.version&gt;20190722&lt;/org.json.version&gt;
&lt;swagger.version&gt;2.9.2&lt;/swagger.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-elasticsearch&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;mysql&lt;/groupId&gt;
&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
&lt;artifactId&gt;mapstruct-jdk8&lt;/artifactId&gt;
&lt;version&gt;${mapstruct.version}&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/org.json/json --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.json&lt;/groupId&gt;
&lt;artifactId&gt;json&lt;/artifactId&gt;
&lt;version&gt;${org.json.version}&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/com.alibaba/fastjson
&lt;dependency&gt;
&lt;groupId&gt;com.alibaba&lt;/groupId&gt;
&lt;artifactId&gt;fastjson&lt;/artifactId&gt;
&lt;version&gt;1.2.73&lt;/version&gt;
&lt;/dependency&gt;--&gt;
&lt;dependency&gt;
&lt;groupId&gt;io.springfox&lt;/groupId&gt;
&lt;artifactId&gt;springfox-swagger2&lt;/artifactId&gt;
&lt;version&gt;${swagger.version}&lt;/version&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
&lt;artifactId&gt;mapstruct&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;io.springfox&lt;/groupId&gt;
&lt;artifactId&gt;springfox-swagger-ui&lt;/artifactId&gt;
&lt;version&gt;${swagger.version}&lt;/version&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
&lt;artifactId&gt;mapstruct&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
&lt;artifactId&gt;lombok&lt;/artifactId&gt;
&lt;!--&lt;version&gt;1.18.12&lt;/version&gt;--&gt;
&lt;type&gt;jar&lt;/type&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;
&lt;artifactId&gt;gson&lt;/artifactId&gt;
&lt;version&gt;2.8.6&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;jakarta.validation&lt;/groupId&gt;
&lt;artifactId&gt;jakarta.validation-api&lt;/artifactId&gt;
&lt;version&gt;2.0.2&lt;/version&gt;
&lt;type&gt;jar&lt;/type&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
&lt;artifactId&gt;mapstruct&lt;/artifactId&gt;
&lt;version&gt;1.4.0.Beta3&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;executions&gt;
&lt;execution&gt;
&lt;goals&gt;
&lt;goal&gt;repackage&lt;/goal&gt;
&lt;/goals&gt;
&lt;configuration&gt;
&lt;mainClass&gt;com.example.springmysqlelastic.SpringMysqlElasticApplication&lt;/mainClass&gt;
&lt;/configuration&gt;
&lt;/execution&gt;
&lt;/executions&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
&lt;configuration&gt;
&lt;source&gt;${java.version}&lt;/source&gt;
&lt;target&gt;${java.version}&lt;/target&gt;
&lt;annotationProcessorPaths&gt;
&lt;path&gt;
&lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
&lt;artifactId&gt;mapstruct-processor&lt;/artifactId&gt;
&lt;version&gt;1.4.0.Beta3&lt;/version&gt;
&lt;/path&gt;
&lt;/annotationProcessorPaths&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

PS: I have removed JPA dependency to make it work without spring. Forget it.

You can find that I have changed swagger dependencies to look like below -

&lt;dependency&gt;
&lt;groupId&gt;io.springfox&lt;/groupId&gt;
&lt;artifactId&gt;springfox-swagger2&lt;/artifactId&gt;
&lt;version&gt;${swagger.version}&lt;/version&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
&lt;artifactId&gt;mapstruct&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;io.springfox&lt;/groupId&gt;
&lt;artifactId&gt;springfox-swagger-ui&lt;/artifactId&gt;
&lt;version&gt;${swagger.version}&lt;/version&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
&lt;artifactId&gt;mapstruct&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;

Can you check if it works for you?

Update
After codegen step, this is what it generates -

package com.example.springmysqlelastic.mapper;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import org.springframework.stereotype.Component;
@Generated(
value = &quot;org.mapstruct.ap.MappingProcessor&quot;,
date = &quot;2020-08-11T16:40:57+0530&quot;,
comments = &quot;version: 1.4.0.Beta3, compiler: javac, environment: Java 1.8.0_265 (Private Build)&quot;
)
@Component
public class UserMapperImpl implements UserMapper {
@Override
public UserDTO toUserDTO(User user) {
if ( user == null ) {
return null;
}
UserDTO userDTO = new UserDTO();
return userDTO;
}
@Override
public List&lt;UserDTO&gt; toUserDtos(List&lt;User&gt; users) {
if ( users == null ) {
return null;
}
List&lt;UserDTO&gt; list = new ArrayList&lt;UserDTO&gt;( users.size() );
for ( User user : users ) {
list.add( toUserDTO( user ) );
}
return list;
}
@Override
public User toUser(UserDTO userDTO) {
if ( userDTO == null ) {
return null;
}
User user = new User();
return user;
}
@Override
public List&lt;User&gt; toUsers(List&lt;UserDTO&gt; userDTOS) {
if ( userDTOS == null ) {
return null;
}
List&lt;User&gt; list = new ArrayList&lt;User&gt;( userDTOS.size() );
for ( UserDTO userDTO : userDTOS ) {
list.add( toUser( userDTO ) );
}
return list;
}
}

huangapple
  • 本文由 发表于 2020年8月11日 16:56:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63354852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定