Çok garip bir istisna ile karşılaştım ve sebebini nasıl bulacağımı bilmiyorum.Spring boot devtools ile ilgili bir dozer haritası istisnası
İş dünyası: Mal ekle ve bu arada fiyat listesi, bir malın farklı seviyelerdeki kullanıcı için 5 fiyatı var.
Denetleyicide, önce goodForm'u dozer ile mallara dönüştürün, ardından malları kaydetmek için malServis'i arayın. goodsService yılında mal, kastetmek mal fiyat listesi kaydetme ve
GoodsForm:
@Mapping("priceList")
List<GoodsPriceForm> goodsPriceFormList;
Goods:
List<GoodsPrice> priceList;
Controller:
Goods goods = BeanMapper.map(goodsForm, Goods.class);
goodsService.saveGoods(adminId, goods);
GoodsService:
goodsDao.save(goods);
goods.getPriceList().forEach(p -> p.setGoodsId(goods.getId()));
goodsPriceDao.save(goods.getPriceList());
, mal fiyatına goodsId doldurmak Ama istisna sonrası:
2015-11-27 17:10:57,042 [http-nio-8081-exec-8] ERROR o.a.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: com.foo.goods.model.GoodsPrice cannot be cast to com.foo.goods.model.GoodsPrice] with root cause
java.lang.ClassCastException: com.foo.goods.model.GoodsPrice cannot be cast to com.foo.goods.model.GoodsPrice
at com.foo.goods.service.GoodsService$$Lambda$11/310447431.accept(Unknown Source) ~[na:na]
at java.util.ArrayList.forEach(ArrayList.java:1249) ~[na:1.8.0_51]
at com.foo.goods.service.GoodsService.saveGoods(GoodsService.java:34) ~[classes/:na]
Bu hata mesajı bana çok karışık hissediyorum edelim. Ayrıca bunu tekrarlamak isteyen bir birim testi yazdım ama başarısız oldu.
GoodsForm form = new GoodsForm();
form.setGoodsPriceFormList(Lists.newArrayList(new GoodsPriceForm((byte) 1, BigDecimal.valueOf(10)),
new GoodsPriceForm((byte) 2, BigDecimal.valueOf(9)),
new GoodsPriceForm((byte) 3, BigDecimal.valueOf(8))));
Goods goods = BeanMapper.map(form, Goods.class);
goods.getPriceList().forEach(p -> p.setGoodsId(goods.getId()));
Bu birim sınamasını çalıştırın, tamamlandı. Peki, neden gerçek web durumunda (Spring boot + Jpa) başarısız, ama birim test durumunda, tamam mı?
Controller:
System.out.println("PriceList: " + goods.getPriceList().getClass().getClassLoader());//PriceList: null
System.out.println(goods.getPriceList().get(0).getClass().getClassLoader()); //java.lang.ClassCastException: com.foo.goods.model.GoodsPrice cannot be cast to com.foo.goods.model.GoodsPrice
Ben paketlenmiş kavanoz oluşturulan, o zaman yukarıdaki istisnasız Bu durumda bu kavanoz
java -jar target/myapp.jar
yürütün.
Ve pom.xml'Bu yay çizme-DevTools'un yorumladı ve ardından yukarıdaki istisnasız uygulaması başladı.
senin de aynı sınıfı yüklemek eğer böyle bir istisna vardı sadece zaman:
bakınız
META-INF/spring-devtools.properties
iç kaynaklar klasörünü koymak ve bu içerik eklemek 2 farklı sınıf yükleyiciyle. Her nesnenin sınıf yükleyicisini yazdırmayı deneyebilir misiniz? –Daha sonra aynı sınıf iki farklı sınıf yükleyici tarafından yüklendi. İlk ölçüt, sınıfı bir yerde tek bir kavanoza sokmaktır. –
@Wim Deblauwe Yolunuzu denedim, lütfen bu yazının altındaki ek içeriklerime bakın – zhuguowei