zhuguowei
zhuguowei

Reputation: 8477

A classloader problem related to spring-boot-devtools

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?

Upvotes: 7

Views: 11609

Answers (2)

Vishal Grover
Vishal Grover

Reputation: 9

The same has been asked here.

You need to set the excludeDevTools to false

Upvotes: 0

cLyric
cLyric

Reputation: 259

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

Upvotes: 7

Related Questions