将Map <String,List <String >>映射到Double时,orika“ java.lang.IllegalArgumentException:'r.p ['cp'] {}'不是对象的有效元素属性”

时间:2019-12-08 18:59:08

标签: java orika

我正在使用orika在不同的Java bean之间映射值。

ObjectA-

的定义
    public class ObjectA {

        private Request request;
    }

Request的定义是-

    public class Request {

        private String id;

        private Map<String, List<String>> params;
    }

ObjectB-

的定义
    public class ObjectB {

        private Long reqId;

        private Double cp;

    }

为简洁起见,我省略了设置方法和获取方法。

下面是从objectA映射到ObjectB的代码-

    Map<String,String> fieldMap = new HashMap<>();
    fieldMap.put("request.id","reqId");
    fieldMap.put("request.params['cp']{}", "cp");


    ObjectA a = new ObjectA();
    Request request = new Request();
    request.setId("123");
    a.setRequest(request);


    Map<String, List<String>> params = new HashMap<String, List<String>>();
    params.put("id", new ArrayList<String>(){{add("2");}});
    params.put("cp", new ArrayList<String>(){{add("0.51"); add("0.52"); add("0.53"); }});


    MappingContext.Factory mcf = new MappingContext.Factory();
    MapperFactory mapperFactory = new DefaultMapperFactory.Builder().mapNulls(false).mappingContextFactory(mcf).dumpStateOnException(false).build();
    ClassMapBuilder<ObjectA, ObjectB> impBuilder = mapperFactory.classMap(ObjectA.class, ObjectB.class);
    fieldMap.forEach((k,v) -> impBuilder.field(k,v));
    impBuilder.register();

    BoundMapperFacade<ObjectA, ObjectB> delegate = mapperFactory.getMapperFacade(ObjectA.class, ObjectB.class);

    ObjectB b = delegate.map(a);

代码在github here中。

运行该程序时,我遇到了错误-

  

线程“ main”中的异常java.lang.IllegalArgumentException:'request.params ['cp'] {}'不是ObjectA的有效元素属性            在ma.glasnost.orika.property.PropertyResolver.getElementProperty(PropertyResolver.java:566)            在ma.glasnost.orika.property.PropertyResolver.getProperty(PropertyResolver.java:682)            在ma.glasnost.orika.property.PropertyResolver.getProperty(PropertyResolver.java:638)            在ma.glasnost.orika.metadata.ClassMapBuilder.resolveProperty(ClassMapBuilder.java:873)            在ma.glasnost.orika.metadata.FieldMapBuilder。(FieldMapBuilder.java:78)            在ma.glasnost.orika.metadata.FieldMapBuilder。(FieldMapBuilder.java:61)            在ma.glasnost.orika.metadata.ClassMapBuilder.fieldMap(ClassMapBuilder.java:328)            在ma.glasnost.orika.metadata.ClassMapBuilder.fieldMap(ClassMapBuilder.java:311)            在ma.glasnost.orika.metadata.ClassMapBuilder.field(ClassMapBuilder.java:249)            在mapper.ObjectAToObjectBDemo.lambda $ main $ 0(ObjectAToObjectBDemo.java:36)            在java.util.HashMap.forEach(HashMap.java:1289)            在mapper.ObjectAToObjectBDemo.main(ObjectAToObjectBDemo.java:36)

我想将请求对象params的映射ObjectA的映射cp中“ cp”中的键的第一个元素映射到ObjectB的变量fieldMap.put("request.params['cp']{}", "cp");

我的映射@GetMapping("/form") public ModelAndView createForm(Model model) { model.addAttribute("user", new User()); model.addAttribute("title", "创建用户"); return new ModelAndView("users/form","userModel",model); } 有什么问题,我该如何解决?

注意:我正在使用orika版本1.5.4

0 个答案:

没有答案