MapStrut 新手;对象到字符串错误:
[错误] /util/LicenseMapper.java:[11,23] 无法将属性“java.lang.Object license.customFields[].value”映射到“java.lang.String license.customFields[].value”。考虑声明/实现一个映射方法:“java.lang.String map(java.lang.Object value)”。
代码:
@Mapper
public interface LicenseMapper {
List<License> jsonToDao(List<com.integrator.vo.license.License> source);
}
Run Code Online (Sandbox Code Playgroud)
vo.license 包含具有以下属性的 CustomFields 列表
@SerializedName("Value")
@Expose
private Object value;
Run Code Online (Sandbox Code Playgroud)
Json 将一个字段作为对象输入,因为它可能是布尔值、字符串或任何其他内容,因此我已将其映射到对象中。而在 dao 层中,String 中有相同的字段。(在自定义映射器中,我只是 String.valueof 但不确定如何使用 Mapstrut 实现它)
谁能告诉我 LicenseMapper 中需要进行哪些设置才能将对象转换为字符串?
许可证结构 - 来源和目的地:
.
.
private String notes;
private Boolean isIncomplete;
private List<CustomField> customFields = null;
private List<Allocation> allocations = null;
Run Code Online (Sandbox Code Playgroud)
源中的自定义字段结构(删除了 gson 注释):
.
.
private String name;
private Object dataType;
private Object value;
Run Code Online (Sandbox Code Playgroud)
目的地中的自定义字段结构
private String name;
private …Run Code Online (Sandbox Code Playgroud) 我收到 Mapstruct 异常,其中字段已存在于源和目标中:
@Mapper
public interface DccsMapper {
DccsMapper MAPPER = Mappers.getMapper( DccsMapper.class );
@Mappings({
@Mapping(source = "updatedDate", target = "updatedDate", dateFormat = "yyyy-MM-dd'T'HH:mm:ss"),
})
List<DataCenterCluster> entityListToDaoList(List<Dccs> source);
}
Run Code Online (Sandbox Code Playgroud)
来源:
public class Dccs {
@SerializedName("UpdatedDate")
@Expose
private String updatedDate;
Run Code Online (Sandbox Code Playgroud)
目的地:
public class DataCenterCluster {
@Column(name = "UPDATEDDATE")
private Date updatedDate;
Run Code Online (Sandbox Code Playgroud)
例外:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project Snow: Compilation failure
[ERROR] /C:......../DMapper.java:[18,17] No property named "updatedDate" exists in source parameter(s). Did you mean "empty"?
Run Code Online (Sandbox Code Playgroud)
Pom 还有:
<properties> …Run Code Online (Sandbox Code Playgroud) 我是新来的mapStruct,我不知道如何排除空字段。
这些类看起来像这样:
public class MyClass {
String reference;
Info info;
...
}
public class Info{
Long id;
List<String> parts = new ArrayList<>();
...
}
Run Code Online (Sandbox Code Playgroud)
这是映射器:
@Mapping(target = "info.id", source = "infoId")
public abstract MyClass toMyClass(RequestProto.line Line);
Run Code Online (Sandbox Code Playgroud)
因此,当它info.id为空时,我会使用带有空零件列表的参数MyClass进行实例化。info
MYCLASS(current)
{
reference: "aa",
info: {
parts: []
}
}
Run Code Online (Sandbox Code Playgroud)
我想要的是,当 为info.id空时info,参数为空。
MYCLASS(expected)
{
reference: "aa"
info: null
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何实现这一目标。
我希望我解释了我自己。如果有人能给我带来一些启发,我将不胜感激
如果标题不清楚,我很抱歉,让我通过给出示例代码来澄清:
更新配置文件Dto
public class UpdateProfileDto {
@NotEmpty
private String firstName;
@NotEmpty
private String lastName;
@Size(max = 20)
private String currentPassword;
@Size(max = 20)
private String newPassword;
@Size(max = 20)
private String confirmNewPassword;
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
编码映射
@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
public @interface EncodedMapping {
}
Run Code Online (Sandbox Code Playgroud)
密码编码器映射器
public class PasswordEncoderMapper {
protected final PasswordEncoder passwordEncoder;
public PasswordEncoderMapper(PasswordEncoder passwordEncoder) {
this.passwordEncoder = passwordEncoder;
}
@EncodedMapping
public String encode(String value) {
return passwordEncoder.encode(value);
}
}
Run Code Online (Sandbox Code Playgroud)
用户映射器
@Mapper(config = MapperConfig.class, componentModel = "spring", …Run Code Online (Sandbox Code Playgroud) 我知道我可以创建一个这样的映射器:
@Mapper(componentModel="spring")
public interface MyMapper{
Run Code Online (Sandbox Code Playgroud)
这将导致:
@Component
public class MyMapperImpl{
Run Code Online (Sandbox Code Playgroud)
如何设置获取的bean名称:
@Component("name")
public class MyMapperImpl{
Run Code Online (Sandbox Code Playgroud) 我有一个类层次结构:VehicleDTO是一个基本抽象类。
CarDTO, TruckDTO, VanDTO从它延伸出来。
我在映射器的另一侧有相同的层次结构:
VehicleBO <- CarBO, TruckBO, VanBO。
我希望将所有映射逻辑合并到一个映射器中。时期。
我已经为公共属性定义了映射,但是当它变得有趣时,我在编译过程中得到了这个异常:
The return type ... is an abstract class or interface.
Provide a non abstract / non interface result type or a factory method.
Run Code Online (Sandbox Code Playgroud)
那么,我如何指定一个工厂方法,它基于特定属性的值或 pojo 的类,为我创建一个目标对象?我希望有一个能真正解决问题的好的代码片段。
谢谢!
是否可以在表达式中包含 if-else 或三元运算符之类的条件
@Mapping(expression="java(...)")
Run Code Online (Sandbox Code Playgroud)
我有一个返回 ArrayList 的最后一项的方法,但如果列表为空,它会返回 null。我需要一个条件,所以如果我收到该项目,我可以使用它,或者如果它为空,它将映射为空。
public static MyObjectDetail getLastOne(MyObject myObject) {
List<MyObjectDetail> details = myObject.getMyObjectDetails();
if(details.isEmpty()) {
return null;
} else {
return myObject.getLastDetail(myObject);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我当前使用的@Mapping,如果列表不为空,它就可以正常工作。
@Mapping(expression = "java(MyObjectDetailMapper.getLastOne(myObject).getNumber())", target = "number"),
Run Code Online (Sandbox Code Playgroud) 鉴于这个原型
option java_outer_classname = "FooProto";
message Foo {
string bar = 1;
}
Run Code Online (Sandbox Code Playgroud)
这个java类:
public class MyFoo {
String bar;
}
Run Code Online (Sandbox Code Playgroud)
这个映射器(使用 Mapstruct):
@Mapper
public interface FooMapper() {
FooProto.Foo toProtoFoo(MyFoo myFoo);
}
Run Code Online (Sandbox Code Playgroud)
当我有一个带有空栏的 MyFoo 实例并尝试将其映射到原型时,我得到一个 NullPointerException。
这是因为映射器自动生成的代码调用原型的自动生成方法,如下所示:
public Builder setBar(java.lang.String value) {
if (value == null) {
throw new NullPointerException();
}
bar_ = value;
onChanged();
return this;
}
Run Code Online (Sandbox Code Playgroud)
有什么办法可以避免这个问题吗?(这不涉及在映射之前清理 MyFoo 实例,使其没有空值)
我正在遵循MapStruct 博客上的指南,但在同时使用这 3 种技术时遇到问题。我一直在尝试来自 MapStruct 文档、错误报告、此处帖子的几种方法,但在每种情况下,我最终都会在构建过程中收到以下异常。
有人在 Quarkus 下成功使用 MapStruct 和 Lombok 吗?任何帮助表示赞赏。
compile奇怪的是, a 后的第一个mvn clean总是成功,而第二个compile或运行应用程序会抛出此错误:
Error:(9,8) java: Internal error in the mapping processor: java.lang.RuntimeException:
javax.annotation.processing.FilerException: Attempt to recreate a file for type com.example.service.RawContentDtoMapperImpl
at org.mapstruct.ap.internal.processor.MapperRenderingProcessor.createSourceFile(MapperRenderingProcessor.java:59)
at org.mapstruct.ap.internal.processor.MapperRenderingProcessor.writeToSourceFile(MapperRenderingProcessor.java:39)
...
Run Code Online (Sandbox Code Playgroud)
映射器配置:
Error:(9,8) java: Internal error in the mapping processor: java.lang.RuntimeException:
javax.annotation.processing.FilerException: Attempt to recreate a file for type com.example.service.RawContentDtoMapperImpl
at org.mapstruct.ap.internal.processor.MapperRenderingProcessor.createSourceFile(MapperRenderingProcessor.java:59)
at org.mapstruct.ap.internal.processor.MapperRenderingProcessor.writeToSourceFile(MapperRenderingProcessor.java:39)
...
Run Code Online (Sandbox Code Playgroud)
映射器:
@MapperConfig(componentModel = "cdi")
public interface QuarkusMappingConfig { …Run Code Online (Sandbox Code Playgroud) 这是我的映射器:
@Mapper
public interface ProductMapper {
ProductClassification toProductClassification(ProductTypes pisType);
}
Run Code Online (Sandbox Code Playgroud)
其中ProductTypes和ProductClassification是枚举。我希望它在无法映射枚举时抛出异常,但出现编译器错误:
The following constants from the source enum have no corresponding constant in the target enum and must be be mapped via adding additional mappings: EXTERNAL, UNKNOWN.
我尝试使用@ValueMappings注释,但只能将其配置为将值设置为 null,这是不够的:
@ValueMappings({
@ValueMapping(source = MappingConstants.ANY_REMAINING, target = MappingConstants.NULL)
})
Run Code Online (Sandbox Code Playgroud)
将 MapStruct 映射器配置为在无法映射枚举常量时抛出异常的正确方法是什么?