mapstruct 1.3.1与Spring Boot和Java 13一起使用

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

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

...
		&lt;dependency&gt;
			&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
			&lt;artifactId&gt;lombok&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;!-- Mapping von Entities in DTO --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
			&lt;artifactId&gt;mapstruct&lt;/artifactId&gt;
			&lt;version&gt;${org.mapstruct.version}&lt;/version&gt;
			&lt;scope&gt;compile&lt;/scope&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt; &lt;!-- nessecary? --&gt;
			&lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
			&lt;artifactId&gt;mapstruct-processor&lt;/artifactId&gt;
			&lt;version&gt;${org.mapstruct.version}&lt;/version&gt;
			&lt;scope&gt;compile&lt;/scope&gt;
		&lt;/dependency&gt;
...

			&lt;plugin&gt;
				&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
				&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
				&lt;version&gt;3.8.1&lt;/version&gt; &lt;!-- or newer version --&gt;
				&lt;configuration&gt;
					&lt;source&gt;13&lt;/source&gt; &lt;!-- depending on your project --&gt;
					&lt;target&gt;13&lt;/target&gt; &lt;!-- depending on your project --&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;${org.mapstruct.version}&lt;/version&gt;
						&lt;/path&gt;
						&lt;!-- other annotation processors --&gt;
					&lt;/annotationProcessorPaths&gt;
				&lt;/configuration&gt;
			&lt;/plugin&gt;
enter code here

Mapper Interface

@Mapper(componentModel = &quot;spring&quot;)
public interface ProduktMapper {
    Produkt dto2entity(ProduktDTO produktDTO);
    ProduktDTO entity2dto(Produkt produkt);
}

Unit Test

@Test
public void testEntity2DtoMapper() {
    Produkt produkt = new Produkt();
    produkt.setProduktname(&quot;Testprodukt&quot;);
    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 &gt; Prefrences &gt; 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 &gt; Preferences &gt; Maven &gt; 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 &gt; Maven &gt; 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.

...
	&lt;properties&gt;
		&lt;java.version&gt;13&lt;/java.version&gt;
		&lt;mapstruct.version&gt;1.3.1.Final&lt;/mapstruct.version&gt;
		&lt;lombok.version&gt;1.18.12&lt;/lombok.version&gt;
	&lt;/properties&gt;
...
		&lt;dependency&gt;
			&lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
			&lt;artifactId&gt;mapstruct&lt;/artifactId&gt;
			&lt;version&gt;${mapstruct.version}&lt;/version&gt;
			&lt;scope&gt;compile&lt;/scope&gt;
		&lt;/dependency&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;path&gt;
							&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
							&lt;artifactId&gt;lombok&lt;/artifactId&gt;
							&lt;version&gt;${lombok.version}&lt;/version&gt;
						&lt;/path&gt;
					&lt;/annotationProcessorPaths&gt;
				&lt;/configuration&gt;
			&lt;/plugin&gt;
...

huangapple
  • 本文由 发表于 2020年3月15日 21:40:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/60693512.html
匿名

发表评论

匿名网友

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

确定