简体   繁体   English

如何使用推土机Set <Object> 映射集 <String> ,同时设置 <String> 包含obj.getProperty()

[英]How can I use dozer for Set<Object> mapping Set<String>, while Set<String> contains obj.getProperty()

How can I use dozer for field Set<Object> mapping Set<String> , while Set<String> contains obj.getProperty(). 如何将推土机用于字段Set<Object>映射Set<String> ,而Set<String>包含obj.getProperty()。

I want to map User.roleSet to UserVO.roleNames, which contains Role.name. 我想将User.roleSet映射到包含Role.name的UserVO.roleNames。

public class User {

   private Integer id;

   private Set<Role> roleSet;
}

public class UserVO {

    private Integer id;

    private Set<String> roleNames;
}

public class Role {

    private Integer id;

    private String name;
}

There is a couple of ways to deal with the problem but the first thing that you have to know about that is mapping between collections is little problematic due to Java generics. 有两种方法可以解决该问题,但是您首先要了解的是,由于Java泛型,集合之间的映射几乎没有问题。 They are not available on the runtime and you have to be aware of it. 它们在运行时不可用,您必须意识到这一点。

So in this case on the runtime you will have to collections and based on the collection type you won't be able to determine the collection source type (only by checking some element it will be available). 因此,在这种情况下,您将不得不在运行时进行收集,并且基于收集类型,您将无法确定收集源类型(仅通过检查某些元素是否可用)。

In this case I think the best approach would be define you own custom converter (which you have to register on the dozer config file within custom-converters tags). 在这种情况下,我认为最好的方法是定义您自己的自定义转换器(您必须在custom-converters标记内的推土机配置文件中注册该转换器)。 That part will look like something like that: 该部分看起来像这样:

<configuration>
<custom-converters>
  <converter type="ToRoleNameConverter">
    <class-a>Role</class-a>
    <class-b>java.lang.String</class-b>
  </converter>
</custom-converters>

The source code of that converter: 该转换器的源代码:

public class ToRoleNameConverter extends DozerConverter<Role, String> {

    @SuppressWarnings("unchecked")
    public ToRoleNameConverter() {
       super(Role.class, String.class);
    }

    @Override
    public String convertTo(Role source, String destination) {
        return source.getName();
    }

    @Override
    public Role convertFrom(String source, Role destination) {
       throw new UnsupportedOperationException("Unsupported operation exception!");
   }
}

With that converter you could use define how your basic class and embedded collection should be mapped. 使用该转换器,您可以使用定义如何映射基本类和嵌入式集合。 Additional dozer configuration will be needed: 将需要其他推土机配置:

<mapping>
  <class-a>User</class-a>
  <class-b>UserDto</class-b>
  <field>
    <a>roles</a>
    <b>roleNames</b>
    <a-hint>Role</a-hint>
    <b-hint>java.lang.String</b-hint>
  </field>
</mapping>

With the given configuration you should try to map: 使用给定的配置,您应该尝试映射:

User user = new User()
user.setUserId(1)
user.setRoles(Sets.newHashSet(new Role(1, "admin"), new Role(2, "manager")))

UserDto map = mapper.map(user, UserDto.class)

And get the results: 并得到结果:

User{id=1, roles=[Role{id=2, name=manager}, Role{id=1, name=admin}]}
UserDto{id=1, roleNames=[manager, admin]}

Hope that explain your question! 希望能解释您的问题!

如何转换集合<string>设置<object><div id="text_translate"><p>如何将strRoles转换为Set&lt;Role&gt; 。 先感谢您</p><pre>Set&lt;String&gt; strRoles = signUpRequest.getRoles(); Set&lt;Role&gt; roles = new HashSet&lt;&gt;();</pre><p> <strong>角色.model</strong></p><pre> @Document(collection = "roles") public class Role { @Id private String id; private ERole name; }</pre><p> <strong>Erole.enum</strong></p><pre> public enum ERole { ROLE_ADMIN, ROLE_USER }</pre></div></object></string> - How to convert Set<String> to Set<Object>

暂无
暂无

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

相关问题 推土机深度映射集 <ComplexObject> 设置 <String> - Dozer deep mapping Set<ComplexObject> to Set<String> Set中的推土机映射 <Object> 列出 <ENUM> - Dozer mapping from Set<Object> to List<ENUM> 映射到推土机中的设置映射解决方法 - Map to Set Mapping Workaround in Dozer 使用DOZER映射而不设置方法 - Mapping with DOZER without method set 使用自定义转换器在 Dozer 中设置字符串常量 - Set a constant of string in Dozer using custom converter 推土机从字符串到对象的映射 - Dozer Mapping from string to Object 如果 Set 包含具有某些字符串值的对象,如何检查 Java? - How to check in java if Set contains object with some string value? 如何转换集合<string>设置<object><div id="text_translate"><p>如何将strRoles转换为Set&lt;Role&gt; 。 先感谢您</p><pre>Set&lt;String&gt; strRoles = signUpRequest.getRoles(); Set&lt;Role&gt; roles = new HashSet&lt;&gt;();</pre><p> <strong>角色.model</strong></p><pre> @Document(collection = "roles") public class Role { @Id private String id; private ERole name; }</pre><p> <strong>Erole.enum</strong></p><pre> public enum ERole { ROLE_ADMIN, ROLE_USER }</pre></div></object></string> - How to convert Set<String> to Set<Object> 如何使用编程API告诉Dozer绕过每个字段的空值或空字符串值? - How can I tell Dozer to bypass mapping null or empty string values per field using the programming api? 使用 Java 8,如何为列表中的 obj[4] 设置字符串常量<Object[]>当且仅当它具有空值? - Using Java 8, How to set a String Constant for obj[4] in a List<Object[]> if and only if it has null value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM