Open houyouliang opened 7 years ago
@Test public void testDozerMany() { List<FieldRequest> fieldRequests = new ArrayList<> (); String flightSourceMapping = "{'fieldMappings':[{'classA':'com.psdp.demo.mapping.bean.source.Flight','classB':'com.psdp.demo.mapping.bean.target.FlightInfo','fieldList':[{'a':'flightNo','b':'flightNo','aHint':'','bHint':''},{'a':'flightDetail.flightNo','b':'flightDetailInfo.flightNo','aHint':'','bHint':''},{'a':'flightDetail.deptCode','b':'flightDetailInfo.deptCode','aHint':'','bHint':''}]}]}"; FieldRequest fieldRequest1 = new GsonBuilder().create().fromJson(flightSourceMapping, FieldRequest.class); String userSourceMapping = "{'fieldMappings':[{'classA':'com.psdp.demo.mapping.bean.source.User','classB':'com.psdp.demo.mapping.bean.target.FlightInfo','fieldList':[{'a':'userName','b':'userName','aHint':'','bHint':''},{'a':'userDetail.address','b':'address','aHint':'','bHint':''}]}]}"; FieldRequest fieldRequest2 = new GsonBuilder().create().fromJson(userSourceMapping, FieldRequest.class); String bookSourceMapping = "{'fieldMappings':[{'classA':'com.psdp.demo.mapping.bean.source.Book','classB':'com.psdp.demo.mapping.bean.target.FlightInfo','fieldList':[{'a':'bookName','b':'bookName','aHint':'','bHint':''},{'a':'date','b':'date','aHint':'','bHint':''}]}]}"; FieldRequest fieldRequest3 = new GsonBuilder().create().fromJson(bookSourceMapping, FieldRequest.class); String flightMapping = MappingUtils.getMappings(fieldRequest1); DozerBeanMapper mapper = new DozerBeanMapper(); //mapper.setMappingFiles(myMappingFiles); InputStream is = new ByteArrayInputStream(flightMapping.getBytes()); com.psdp.demo.mapping.bean.target.FlightInfo flightInfo = null; mapper.addMapping(is); try { com.psdp.demo.mapping.bean.source.Flight flight = new com.psdp.demo.mapping.bean.source.Flight(); FlightDetail flightDetail = new FlightDetail(); flightDetail.setDeptCode("PEK"); flightDetail.setFlightNo("MU3939"); flight.setFlightDetail(flightDetail); flight.setFlightNo("MU2191"); flightInfo= new com.psdp.demo.mapping.bean.target.FlightInfo(); //Class<?> clazzTarget = classLoaderTarget.loadClass("com.flight.Msg"); mapper.map(flight, flightInfo); //Object obj = mapper.map(sourceObject, clazzTarget); System.out.println(new GsonBuilder().create().toJson(flightInfo)); //System.out.println(new GsonBuilder().create().toJson(obj)); } catch (Exception e) { e.printStackTrace(); } String userMapping = MappingUtils.getMappings(fieldRequest2); DozerBeanMapper mapper1 = new DozerBeanMapper(); //mapper.setMappingFiles(myMappingFiles); InputStream is1 = new ByteArrayInputStream(userMapping.getBytes()); mapper1.addMapping(is1); try { com.psdp.demo.mapping.bean.source.User user = new com.psdp.demo.mapping.bean.source.User(); UserDetail userDetail = new UserDetail(); userDetail.setAddress(""); user.setUserDetail(userDetail); user.setUserName(""); //Class<?> clazzTarget = classLoaderTarget.loadClass("com.flight.Msg"); mapper1.map(user, flightInfo); //Object obj = mapper.map(sourceObject, clazzTarget); System.out.println(new GsonBuilder().create().toJson(flightInfo)); //System.out.println(new GsonBuilder().create().toJson(obj)); } catch (Exception e) { e.printStackTrace(); } String bookMapping = MappingUtils.getMappings(fieldRequest3); DozerBeanMapper mapper2 = new DozerBeanMapper(); //mapper.setMappingFiles(myMappingFiles); InputStream is2 = new ByteArrayInputStream(bookMapping.getBytes()); mapper2.addMapping(is2); try { com.psdp.demo.mapping.bean.source.Book book = new com.psdp.demo.mapping.bean.source.Book(); book.setBookName("Thinking in java"); book.setDate(new Date()); //Class<?> clazzTarget = classLoaderTarget.loadClass("com.flight.Msg"); mapper2.map(book, flightInfo); //Object obj = mapper.map(sourceObject, clazzTarget); System.out.println(new GsonBuilder().create().toJson(flightInfo)); //System.out.println(new GsonBuilder().create().toJson(obj)); } catch (Exception e) { e.printStackTrace(); } }