Ambiguous constructors found for creating com.app.test.UserInfo. Either declare parameterless
constructor or annotate the default constructor with an annotation named @Default.
方法一、手动添加显示构造无参方法
当你的 POJO 里有其他构造函数时,记得要手动添加无参构造函数
注意:禁止使用 Lombok @NoArgsConstructor 注解,Mapstruct 貌似检测不到噢!
方法二、添加 @Default 注解
package foo.support.mapstruct;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.CONSTRUCTOR)
@Retention(RetentionPolicy.CLASS)
public @interface Default {
}
@Default
public UserInfo(String username, String area) {
//...
}
本文介绍了如何在Mapstruct中处理POJO对象的构造函数冲突,通过添加无参构造函数和使用@Default注解来确保默认实例化。两种方法分别适用于不同场景,避免了Lombok的局限性。
1002

被折叠的 条评论
为什么被折叠?



