nullValueCheckStrategy

nullValueCheckStrategy

是否可以在接口Mapper.java中添加新属性(如nullValueCheckStrategy)以检查权限?

如果我添加了nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS
代码生成将为每个字段添加if(filed != null)

而且我想有一个if(rightService.hasRole("Admin") { ... }的if,以避免为没有权限的用户更新值。

这是我第一次在这里提出问题,希望得到一些答案,非常感谢。

最佳答案

当前不支持此功能。但是,您可以通过编写自定义映射器并为拥有的不同角色提供不同的方法来实现。您的映射器可能如下所示:

@Mapper
public abstract class MyMapper {

    private MyService service;

    public void update(Target target, Source source) {
        if (service.hasRole("Admin")) {
            updateForAdmin(target, source);
        } else if (service.hasRole("X")) {
            updateForX(target, source);
        }
    }


    //Mappings for Admin, ignore those that he / she has no rights
    protected abstract updateForAdmin(@MappingTarget Target target, Source source);

    //Mappings for X, ignore those that he / she has no rights
    protected abstract updateForX(@MappingTarget Target target, Source source);

    //setter injection
}


基本思想是为每个角色提供一种具有适当映射的方法。映射器只有一个公共方法,其余的都在这里供MapStruct实现。

关于java - Java-MapStruct:Mapper的自定义属性,例如`nullValueCheckStrategy`,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47995171/

10-11 17:12