7

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?

Guerric P
  • 30,447
  • 6
  • 48
  • 86
zhuguowei
  • 8,401
  • 16
  • 70
  • 106

2 Answers2

7

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

cLyric
  • 259
  • 2
  • 6
  • I have the same issue, but not in a Spring context. I am using Liferay Developer Studio (which is basically an enhanced Eclipse) and using Orika instead of Dozer. Do you know how should I go in order to solve it ? – Victor Nov 22 '20 at 15:52
0

The same has been asked here.

You need to set the excludeDevTools to false

Prateek Gupta
  • 2,422
  • 2
  • 16
  • 30
  • If the linked answer also answers this queastion you should vote to close this question as a duplicate. – piet.t Jun 02 '20 at 13:22