问题
Background: Spring boot project, add goods and goods price list
Goods:
List<GoodsPrice> pricelist;
in controller first convert goodsForm to goods(by dozer), then save goods,after saving goods iterate goods price list to populate goodsId.
goods.getPriceList().forEach(p -> p.setGoodsId(goods.getId()));
When iterate goods price list, throw exception:
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]
Somebody remind me this exception related to classloader, and in eclipse debug mode, I outputed the GoodsPrice's ClassLoader:
sun.misc.Launcher$AppClassLoader@14dad5dc
and goods:
org.springframework.boot.devtools.restart.classloader.RestartClassLoader@591c6338
Indeed exist diff classloader.
Then I commented spring-boot-devtools
then tried again this time it's ok.
So if still retain spring-boot-devtools
, how to solve this problem?
回答1:
Dozer is using the wrong classloader.
You can solve it adding this file in your resource folder :
META-INF/spring-devtools.properties
with inside :
restart.include.dozer=/dozer-5.5.1.jar
restart.include.dozer-spring=/dozer-spring-5.5.1.jar (only if you use this jar!)
sources : http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-devtools-customizing-classload
来源:https://stackoverflow.com/questions/33957612/a-classloader-proplem-related-to-spring-boot-devtools