MapStruct implementation is not working in Spring Boot Web Application

前提是你 提交于 2019-12-04 11:44:26

First of all, public UserAndEmployeeMapper INSTANCE = Mappers.getMapper( UserAndEmployeeMapper.class ); should only be used with the default component model, otherwise you risk to have the UserAndEmployeeMapper not correctly initialized.

The UserAndEmployeeMapper in your LoginServiceImpl must be annotated with @Autowired, otherwise it cannot be injected by Spring, and that's why it is null.

I don't know your package structure. If your Spring Boot application class in the package org then it will pick up the UserAndEmployeeMapperImpl. Otherwise make sure that the spring configuration picks up the UserAndEmployeeMapperImpl.

If everything from above is correctly setup and you are starting the application via an IDE make sure that target/generated-sources or the alternative for Gradle is part of your sources and is picked up. Have a look at the IDE Support to make sure that you have correctly setup the Annotation processor discovery for an IDE. For example, IntelliJ will not invoke the MapStruct Annotation Processor with your current setup, it doesn't pick up the annotationProcessorPaths from the maven compiler.

I ran into this issue too, and changing the mapper from abstract class to an interface didn't work for me.

I ended up:

  • Removing the INSTANCE property from the mapper interface
  • Creating a class implementing the interface, implementing the mappes methods
  • Adding an instance of this class as an @Autowired property in classes which need the mapper, and calling that instead.

WebAppMapper.java

    @Mapper(componentModel = "spring")
    public interface WebAppMapper {
        CatalogData createCatalogRequestToData(CreateCatalogRequest createCatalogRequest);
    }

WebAppMapperImpl.java

@Component
public class WebAppMapperImpl implements WebAppMapper {
    @Override
    public CatalogData createCatalogRequestToData(CreateCatalogRequest createCatalogRequest) {
        CatalogData catalogData = new CatalogData();
        catalogData.setId(createCatalogRequest.getId());
        catalogData.setName(createCatalogRequest.getName());
        return catalogData;
    }
}

CatalogController.java

@Autowired
WebAppMapper webAppMapper;

webAppMapper.createCatalogRequestToData(createCatalogRequest);

This worked for me.

springfox-swagger2 dependency is loading older version of mapstruct after excluding it and adding specific version of mapstruct dependency in pom.xml it started generating sources

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
<exclusions>
 <exclusion>
 <groupId>org.mapstruct</groupId>
  <artifactId>mapstruct</artifactId>
 </exclusion>
</exclusions>

Added below map struct dependency

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.3.0.Beta1</version>

Making abstract class as an interface worked for me.

public interface UserAndEmployeeMapper {

For making all mapper classes to qualify as spring bean add following compilerArgs to your maven-compiler-plugin.

<compilerArgs>
<arg>-Amapstruct.suppressGeneratorTimestamp=true</arg>
<arg>-Amapstruct.defaultComponentModel=spring</arg>
</compilerArgs> 

if using maven-processor-plugin add following options

<options>                       
<mapstruct.suppressGeneratorTimestamp>
true
</mapstruct.suppressGeneratorTimestamp>                      
<mapstruct.defaultComponentModel>
spring
</mapstruct.defaultComponentModel>
</options>

In my case, I believe the error was due to an incomplete build.gradle.

Before

dependencies {
    compile group: 'org.mapstruct', name: 'mapstruct-processor', version: '1.2.0.Final'
}

After

apply plugin: 'net.ltgt.apt'

dependencies {
    compile group: 'org.mapstruct', name: 'mapstruct-processor', version: '1.2.0.Final'
    compile group: 'org.mapstruct', name: 'mapstruct-jdk8', version: '1.2.0.Final'
    apt 'org.mapstruct:mapstruct-processor:1.2.0.Final'
}


buildscript {
    repositories {
        mavenCentral()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath("net.ltgt.gradle:gradle-apt-plugin:0.9")
    }
}

Ensure you have configured your build.gradle properly, as described in the docs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!