I checked the stackhoverflow for this question and there is no standard answer.
version info: JDK21 and Kotlin 1.9.10 and Kotlin NoArg Plugin
This is my pom.xml file:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.4</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<kotlin.version>1.9.10</kotlin.version>
</properties>
build plugin info:
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<configuration>
<compilerPlugins>
<plugin>no-arg</plugin>
</compilerPlugins>
<pluginOptions>
<option>no-arg:annotation=com.wansensoft.NoArg</option>
</pluginOptions>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-noarg</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
<!-- other plugin -->
</plugin>
</plugins>
</build>
my data class in kotlin code
@NoArg
data class SupplierVO (
@JsonFormat(shape = JsonFormat.Shape.STRING)
val id: Long,
@ExcelExport(value = "供应商名称*", sort = 1)
val supplierName: String,
@ExcelExport(value = "联系人*", sort = 2)
val contact: String?,
@ExcelExport(value = "联系电话", sort = 4)
val contactNumber: String?,
@ExcelExport(value = "手机号码*", sort = 3)
val phoneNumber: String?,
@ExcelExport(value = "", sort = 22)
val address: String?,
@ExcelExport(value = "电子邮箱", sort = 5)
val email: String?,
// other filed
)
my object in java code
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("supplier")
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Supplier implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@TableId(value = "id")
private Long id;
private Long tenantId;
private String supplierName;
private String contact;
private String contactNumber;
private String phoneNumber;
private String address;
private String email;
}
This is the code I use for BeanUtils.copyProperties()
in java
List<Supplier> suppliers = supplierService.list();
List<SupplierVO> supplierVOS = new ArrayList<>();
suppliers.forEach(item -> {
var supplierVO = new SupplierVO();
BeanUtils.copyProperties(item, supplierVO);
supplierVOS.add(supplierVO);
});
SupplierVO
is my parameterless constructor. When I use the BeanUtils.copyProperties
method, I am unable to copy Java data objects to kotlin data class objects. I am sure there is data in the suppliers
list because I saw it in debug mode.
Comment From: Jzow
package com.wansensoft
annotation class NoArg
This is my kotlin annotation, which I referred to the official documentation for, so I ensure that there are no issues with this annotation.
I also tried lombok and it didn't work, I'm not sure if there's a problem with my new version of kotlin, so I tried using kotlin no arg plugin
to copy through BeanUtils.copyProperties
.
I noticed the following line
Method writeMethod=targetPd. getWriteMethod();
This writeMethod object is empty. My understanding is that Java reflection cannot run kotlin
Comment From: Jzow
Resolved that this is not a bean copy issue