英文:
mapstruct 1.3.1 with boot spring &java 13
问题
...
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- Mapping von Entities in DTO -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
<scope>compile</scope>
</dependency>
<dependency> <!-- nessecary? -->
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
<scope>compile</scope>
</dependency>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <!-- or newer version -->
<configuration>
<source>13</source> <!-- depending on your project -->
<target>13</target> <!-- depending on your project -->
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<!-- other annotation processors -->
</annotationProcessorPaths>
</configuration>
</plugin>
...
@Mapper(componentModel = "spring")
public interface ProduktMapper {
Produkt dto2entity(ProduktDTO produktDTO);
ProduktDTO entity2dto(Produkt produkt);
}
@Test
public void testEntity2DtoMapper() {
Produkt produkt = new Produkt();
produkt.setProduktname("Testprodukt");
ProduktDTO produktDTO = mapper.entity2dto(produkt);
assertEquals(produkt.getProduktname(), produktDTO.getProduktname());
}
英文:
I'm trying to configure mapstrcut 1.3.1 with maven and STS (Eclipse) and based on Java 13. I've tried several configurations in my pom.xml, including the use of maven-compiler-plugin
or alternatively maven-processor-plugin
from org.bsc.maven. Mapstruct does not generate any source classes and therefore my test leads to a ClassNotFoundException since no implementation of the mapper interface can be found. Does anybody have a successful config running mapstruct with java 13 in spring boot?
pom.xml
...
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- Mapping von Entities in DTO -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
<scope>compile</scope>
</dependency>
<dependency> <!-- nessecary? -->
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
<scope>compile</scope>
</dependency>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <!-- or newer version -->
<configuration>
<source>13</source> <!-- depending on your project -->
<target>13</target> <!-- depending on your project -->
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<!-- other annotation processors -->
</annotationProcessorPaths>
</configuration>
</plugin>
enter code here
Mapper Interface
@Mapper(componentModel = "spring")
public interface ProduktMapper {
Produkt dto2entity(ProduktDTO produktDTO);
ProduktDTO entity2dto(Produkt produkt);
}
Unit Test
@Test
public void testEntity2DtoMapper() {
Produkt produkt = new Produkt();
produkt.setProduktname("Testprodukt");
ProduktDTO produktDTO = mapper.entity2dto(produkt);
assertEquals(produkt.getProduktname(), produktDTO.getProduktname());
}
答案1
得分: 2
你尝试过先从Maven编译吗?
在我的案例中,使用Maven编译时可以工作,但作为Spring Boot应用程序启动时失败了。
经过一些搜索,我发现Eclipse必须配置正确的Maven连接器。
如果在“窗口 > 首选项 > Maven”中缺少“注解处理”条目,这意味着必须下载Maven连接器。
为了安全起见,在继续之前,请检查是否安装了此插件:m2e-apt。
然后打开“窗口 > 首选项 > Maven > 发现”,点击“打开目录”。搜索“jdt”,你将看到一个要从列表中下载的连接器。
安装后,重新启动,然后回到“首选项 > Maven > 注解处理”(之前缺失的选项卡!),选择“自动配置JDT APT”,并确认所有后续对话框。
现在所有的错误应该都消失了,项目应该可以编译!
英文:
Have you tried to compile from maven first?
In my case it was working when compiled with maven, but it was failing when launched as spring boot application.
After some search I've found out that eclipse must have the correct maven connector configured.
If into Window > Prefrences > Maven
the Annotation Processing
entry is missing, that means that the maven connector must be downloaded.
To be safe, before to proceed check that you have this plugin installed: m2e-apt.
Then open Window > Preferences > Maven > Discovery
, click on "Open Catalog". Search "jdt", and you will see one connector to download from the list.
Install, reboot, and go back into Preferences > Maven > Annotation Processing
(the tab that was missing!), select "Automatically configure JDT APT", and confirm all the following dialogs.
Now all the errors should be gone, and the project should compile!
答案2
得分: 1
以下是我为您翻译的内容:
这是适用于我的Spring Boot 2.2.5应用程序的配置,使用Java 13,MapStruct 1.3.1和Lombok 1.18.12。
...
<properties>
<java.version>13</java.version>
<mapstruct.version>1.3.1.Final</mapstruct.version>
<lombok.version>1.18.12</lombok.version>
</properties>
...
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
<scope>compile</scope>
</dependency>
...
<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>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
...
英文:
This is the configuration that works for my spring boot 2.2.5 application with Java 13, mapstruct 1.3.1 and lombok 1.18.12.
...
<properties>
<java.version>13</java.version>
<mapstruct.version>1.3.1.Final</mapstruct.version>
<lombok.version>1.18.12</lombok.version>
</properties>
...
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
<scope>compile</scope>
</dependency>
...
<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>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论