哪个注解可以让dozer包的mapper接口中的方法不读取指定get方法

使用dozer包下的mapper的实现类的map方法去进行类之间的属性值赋值,似乎基于getter方法,有什么注解能让被修饰的方法不被当作getter方法?因为实际开发过程中有的get方法确实没有对应的属性,是专门获取处理结果用的

在 Dozer 中,可以使用 @Mapping 注解来指定类属性的映射关系,该注解可以放在属性的 getter 方法上或者属性上。默认情况下,如果一个属性有 getter 方法,则 Dozer 将其视为一个可读属性,而如果有 setter 方法,则将其视为可写属性。

如果你有一些 getter 方法不是用于获取属性值,而是用于其他用途,你可以在这些方法上添加 @Mapping 注解并指定其 ignore 属性为 true,示例如下:

public class Source {
    private String name;
    
    // getter 方法不会被当做属性的 getter 方法处理
    public String getProcessedName() {
        // ...
    }
    
    // setter 方法
    public void setName(String name) {
        this.name = name;
    }
}

public class Destination {
    private String name;
    
    // setter 方法
    public void setName(String name) {
        this.name = name;
    }
}

// 映射配置
Mapper mapper = DozerBeanMapperBuilder.create()
    .withMapping(builder -> {
        builder.mapping(Source.class, Destination.class)
            .fields("processedName", "name",
                MappingBuilder.oneWay(),
                MappingBuilder.ignore(true));
    })
    .build();

// 进行映射
Source source = new Source();
source.setName("foo");

Destination destination = mapper.map(source, Destination.class);
System.out.println(destination.getName()); // 输出 "foo"


在上面的代码中,@Mapping 注解被放置在 Source 类的 getProcessedName 方法上,并指定其 ignore 属性为 true,这样 Dozer 就不会将其作为属性的 getter 方法处理了。然后,使用 fields 方法来指定属性的映射关系,将 processedName 映射到 name,并使用 ignore 方法来忽略 processedName 的 getter 方法。最后,通过 mapper.map 方法进行映射,得到目标对象 Destination。答案参考来自 https://www.wodianping.com/