简体   繁体   English

MapStruct:将目标字段类型用作对象时发生错误

[英]MapStruct: error occur when using Target field type as Object

When I tried for MapStruct to convert type to Object, it didn't work well.当我尝试让 MapStruct 将类型转换为 Object 时,效果不佳。

Target Class目标班级

public static class Filterable implements Name,Disassemble,Note{
    String name;
    String disassemble;
    Object note; // <-- Object type

    ... getter setter
}

Mapping abstract class映射抽象类

@Mappings({
        @Mapping(source = "s",target = "name"),
        @Mapping(source = "s",target = "note"), // <--
        @Mapping(source = "s",target = "disassemble", qualifiedByName = "toDisassemble")
})
public abstract UiDto.Option.Filterable toUiFilterableOption(String s);

Result when I compile我编译时的结果

@Override
public Filterable toUiFilterableOption(String s) {
    if ( s == null ) {
        return null;
    }

    Filterable filterable = new Filterable();

    filterable.setName( s );
    filterable.setNote( toUiFilterableOption( s ) ); // <-- hear is problem
    filterable.setDisassemble( Converter.toDisassemble( s ) );

    return filterable;
}

How could I solve this problem?我怎么能解决这个问题?

MapStruct tries to find a method that maps from the source class to the target class: Object map(String source) . MapStruct 试图找到一种从源类映射到目标类的方法: Object map(String source)

As far as the target class is Object , the method annotated with @Mapping itself has suitable method signature: Filterable toUiFilterableOption(String s) .只要目标类是Object ,用@Mapping注释的方法本身就有合适的方法签名: Filterable toUiFilterableOption(String s) Because Filterable just like any other class in Java is an Object .因为Filterable就像 Java 中的任何其他类一样是一个Object And the method argument also is a String .并且方法参数也是一个String

To solve this issue use qualifiedByName in @Mapping and add a mapping method annotated with @Named :要解决此问题,请在@Mapping使用qualifiedByName并添加用@Named注释的映射方法:

@Mapper
public abstract class TestMapper {

  public static final TestMapper TEST_MAPPER = Mappers.getMapper(TestMapper.class);

  @Mapping(source = "s", target = "name")
  @Mapping(source = "s", target = "note", qualifiedByName = "toNote")
  @Mapping(source = "s", target = "disassemble", qualifiedByName = "toDisassemble")
  public abstract Filterable toUiFilterableOption(String s);

  @Named("toNote")
  protected Object toNote(String s) {
    return s.toUpperCase(); //just as an example
  }

  //...
}

The generated code looks like this:生成的代码如下所示:

public class TestMapperImpl extends TestMapper {

  @Override
  public Filterable toUiFilterableOption(String s) {
    if (s == null) {
      return null;
    }

    Filterable filterable = new Filterable();

    filterable.setName(s);
    filterable.setNote(toNote(s));
    filterable.setDisassemble(toDisassemble(s));

    return filterable;
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当目标是内联对象时如何使用 Mapstruct? - How to use Mapstruct when target is inline object? Mapstruct:基于鉴别字段的抽象目标类和具体类型 - Mapstruct : abstract target class and concrete type based on discriminator field MapStruct根据目标类型映射对象的正确实例 - MapStruct map correct instance of object based on target type 如何将字符串类型的 map 源 object 用于 MapStruct 中的目标 UUID? - How to map the source object of type String to target UUID in MapStruct? 需要使用mapstruct将两个源对象合并到目标对象中,其中source1中的一个字段具有List,而source2中的一个字段为string - Need to merge two source objects into target object using mapstruct ,here one field in source1 has List and in source2 that is string Mapstruct 将字段转换为对象字段 - Mapstruct convert field to object field 当所有字段为空时如何配置mapstruct以忽略地图对象 - How to configurate mapstruct to ignore map object when all field are null Object MapStruct List 映射到 List 时的字段映射问题 - Object field mapping problem when mapping MapStruct List to List Mapstruct:在尝试 map 嵌入 object 内的字段时返回 null - Mapstruct: Returning null when trying to map a field inside an embedded object Java- Mapstruct:从源到目标的特定字段映射,该字段位于列表 Object 内 - Java- Mapstruct : Mapping of specific field from source to target which is inside a List Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM