My mapperImpl by mapstruct is not generating getters and setters
i would like to know why my mapperImpl generated by mapstruct isnt being generated with getters and setters.
I think that because of this, when i go make a GET request on postman it returns all null what shouldnt be returning null.
Im using maven, java 8 and spring-boot
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.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.celulaweb</groupId>
<artifactId>crud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>crud</name>
<description>Crud Estudo CelulaWeb</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.4.1.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.4.1</version>
<configuration>
<outputChangeLogFile>src/main/resources/liquibase-outputChangeLog.xml</outputChangeLogFile>
<url>jdbc:mysql://localhost/crud</url>
<username>root</username>
<password>root</password>
</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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.4.1.Final</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Mapper:
package com.celulaweb.crud.service.mappers;
import com.celulaweb.crud.domain.Cidade;
import com.celulaweb.crud.service.DTO.CidadeDTO;
import org.mapstruct.Mapper;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@Mapper(componentModel = "spring")
public interface CidadeMapper {
CidadeDTO cidadeParaDTO(Cidade cidade);
List<CidadeDTO> cidadeParaDTO(List<Cidade> cidade);
Cidade cidadeDTOParaCidade(CidadeDTO cidadeDTO);
}
EntittyDTO:
package com.celulaweb.crud.service.DTO;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
@Getter
@Setter
public class CidadeDTO implements Serializable {
private Long id;
private String nome;
private Integer qtdHabitantes;
private String estado;
}
Entity:
package com.celulaweb.crud.domain;
import com.sun.istack.NotNull;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Size;
@Entity
@Table(name = "cidade")
@Getter
@Setter
public class Cidade {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@Size(min = 3, max = 100)
private String nome;
private Integer qtdHabitantes;
@NotNull
private String estado;
@Override
public boolean equals(Object obj) {
if (obj == null) { return false; }
if (obj == this) { return true; }
if (obj.getClass() != getClass()) {
return false;
}
Cidade rhs = (Cidade) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.append(id, rhs.id)
.isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder().append(id).toHashCode();
}
}
mapperImpl:
package com.celulaweb.crud.service.mappers;
import com.celulaweb.crud.domain.Cidade;
import com.celulaweb.crud.service.DTO.CidadeDTO;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import org.springframework.stereotype.Component;
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2022-01-20T13:49:14-0300",
comments = "version: 1.4.1.Final, compiler: javac, environment: Java 11.0.13 (Ubuntu)"
)
@Component
public class CidadeMapperImpl implements CidadeMapper {
@Override
public CidadeDTO cidadeParaDTO(Cidade cidade) {
if ( cidade == null ) {
return null;
}
CidadeDTO cidadeDTO = new CidadeDTO();
return cidadeDTO;
}
@Override
public List<CidadeDTO> cidadeParaDTO(List<Cidade> cidade) {
if ( cidade == null ) {
return null;
}
List<CidadeDTO> list = new ArrayList<CidadeDTO>( cidade.size() );
for ( Cidade cidade1 : cidade ) {
list.add( cidadeParaDTO( cidade1 ) );
}
return list;
}
@Override
public Cidade cidadeDTOParaCidade(CidadeDTO cidadeDTO) {
if ( cidadeDTO == null ) {
return null;
}
Cidade cidade = new Cidade();
return cidade;
}
}
If someone know how to solve, please help. Thanks ;)
Solution 1:
If you want to use mapstruct together with lombok you have to include lombok-mapstruct-binding in your pom.xml
have a look at "Can I use MapStruct together with Project Lombok?" under https://mapstruct.org/faq/
like this:
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.4.1.Final</version>
</path>
</annotationProcessorPaths>