ModelMapper:如何将java.util.Map映射到POJO?

dmz*_*z73 -2 java modelmapper

如何将java.util.Map映射到POJO?

以下测试给出了配置错误.

似乎打开了一个类似的问题,但它没有得到解决https://github.com/jhalterman/modelmapper/issues/116.

测试:

package org.me.modelmapper;

import java.util.HashMap;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class MapperTest {


    private ModelMapper modelMapper;


    public MapperTest() {
    }


    public static class Foo {

        public String a;
        public String b;

        public String getA() {
            return a;
        }

        public void setA(String a) {
            this.a = a;
        }

        public String getB() {
            return b;
        }

        public void setB(String b) {
            this.b = b;
        }

    }


    @BeforeMethod
    public void setUpMethod() throws Exception {
        modelMapper = new ModelMapper();
        modelMapper.getConfiguration()
                .setFieldMatchingEnabled(true);
    }

    @Test
    public void shouldMapHashMapToFoo() {
        HashMap<String, String> map = new HashMap<>();
        map.put("a", "aaaa");
        map.put("b", "bbbb");

        PropertyMap<HashMap<String, String>, Foo> fooMap = new PropertyMap<HashMap<String, String>, MapperTest.Foo>() {

            @Override
            protected void configure() {
                map(source.get("a"), destination.a);
                map(source.get("b"), destination.b);
            }

        };

        modelMapper.addMappings(fooMap);

        Foo foo = modelMapper.map(map, Foo.class);

        Assert.assertEquals(foo.getA(), map.get("a"));
        Assert.assertEquals(foo.getB(), map.get("b"));

    }

}
Run Code Online (Sandbox Code Playgroud)

错误:

Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@60acc399
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 2.326 sec <<< FAILURE!
shouldMapHashMapToFoo(org.me.modelmapper.MapperTest)  Time elapsed: 0.186 sec  <<< FAILURE!
org.modelmapper.ConfigurationException: ModelMapper configuration errors:

1) Invalid source method java.util.HashMap.get(). Ensure that method has zero parameters and does not return void.

2) Invalid source method java.util.HashMap.get(). Ensure that method has zero parameters and does not return void.

2 errors
    at org.modelmapper.internal.Errors.throwConfigurationExceptionIfErrorsExist(Errors.java:241)
    at org.modelmapper.internal.ExplicitMappingBuilder.visitPropertyMap(ExplicitMappingBuilder.java:228)
    at org.modelmapper.PropertyMap.configure(PropertyMap.java:380)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.modelmapper.internal.ExplicitMappingBuilder.build(ExplicitMappingBuilder.java:194)
    at org.modelmapper.internal.TypeMapImpl.addMappings(TypeMapImpl.java:72)
    at org.modelmapper.internal.TypeMapStore.getOrCreate(TypeMapStore.java:101)
    at org.modelmapper.ModelMapper.addMappings(ModelMapper.java:93)
    at org.me.modelmapper.MapperTest.shouldMapHashMapToFoo(MapperTest.java:354)


Results :

Failed tests:   shouldMapHashMapToFoo(org.me.modelmapper.MapperTest): ModelMapper configuration errors:
(..)

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
Run Code Online (Sandbox Code Playgroud)

big*_*Guy 9

它会自动映射.你并不需要配置任何映射.这将有效:

@Test
public void shouldMapHashMapToFoo() {
    HashMap<String, String> map = new HashMap<>();
    map.put("a", "aaaa");
    map.put("b", "bbbb");

    Foo foo = modelMapper.map(map, Foo.class);

    Assert.assertEquals(foo.getA(), map.get("a"));
    Assert.assertEquals(foo.getB(), map.get("b"));

}
Run Code Online (Sandbox Code Playgroud)

编辑: 如果您需要将HashMap映射到更复杂的结构,您可以始终使用"中级"类.

  public static class Bar
  {
    private String Z;
    private String W;

    public String getZ()
    {
      return Z;
    }

    public void setZ(String z)
    {
      Z = z;
    }

    public String getW()
    {
      return W;
    }

    public void setW(String w)
    {
      W = w;
    }
  }

  @Test
  public void shouldMapHashMapToFoo()
  {
    HashMap<String, String> map = new HashMap<>();
    map.put("z", "aaaa");
    map.put("w", "bbbb");

    Bar bar = modelMapper.map(map, Bar.class);

    PropertyMap<Bar, Foo> barToFooMap = new PropertyMap<Bar, Foo>()
    {
      @Override
      protected void configure()
      {
        map().setA(source.getZ());
        map().setB(source.getW());
      }

    };
    modelMapper.addMappings(barToFooMap);

    Foo foo = modelMapper.map(bar, Foo.class);

    Assert.assertEquals(foo.getA(), map.get("z"));
    Assert.assertEquals(foo.getB(), map.get("w"));
  }
Run Code Online (Sandbox Code Playgroud)

  • 它不受支持.你可以在源代码中看到它.检查类PropertyInfoResolver,第49行.关键部分:method.getParameterTypes().length == 0这意味着如果你的getter有> 0参数,则getter方法无效.使用我提供的解决方法,或"修复"lib. (3认同)