java – 与Spring引导devtools相关的推土机映射异常

前端之家收集整理的这篇文章主要介绍了java – 与Spring引导devtools相关的推土机映射异常前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我遇到了一个非常奇怪的异常,我不知道如何找到原因.

经营背景:
添加商品,同时它的价格表,商品有差价水平用户的5个价格.

在控制器中,首先使用dozer将goodForm转换为商品,然后调用goodsService来保存商品.
在货物服务后保存货物,遍历货物价格表并填充货物ID到货物价格,

@H_301_8@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());

但它抛出异常:

@H_301_8@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]

这个错误信息让我感到非常困惑.另外我写一个单元测试想重复这个,但失败了.

@H_301_8@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()));

运行此单元测试,执行正常.
那么为什么在真实的网络情况下(Spring boot Jpa)它失败了,但是在单元测试情况下它还可以吗?

@H_301_8@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

如果我生成了一个打包的jar,那么执行这个jar

@H_301_8@java -jar target/myapp.jar

在这种情况下没有上述例外.

我在pom.xml中评论了spring-boot-devtools,然后启动了应用程序,没有异常.

解决方法

By default,any open project in your IDE will be loaded using the “restart” classloader,and any regular .jar file will be loaded using the “base” classloader. If you work on a multi-module project,and not each module is imported into your IDE,you may need to customize things. To do this you can create a Meta-INF/spring-devtools.properties file.

The spring-devtools.properties file can contain restart.exclude. and restart.include. prefixed properties. The include elements are items that should be pulled-up into the “restart” classloader,and the exclude elements are items that should be pushed down into the “base” classloader. The value of the property is a regex pattern that will be applied to the classpath.

我的解决方案:将Meta-INF / spring-devtools.properties放在resources文件夹中,并添加内容

@H_301_8@restart.include.dozer=/dozer-5.5.1.jar

请参阅:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-devtools-customizing-classload

原文链接:https://www.f2er.com/java/129418.html

猜你在找的Java相关文章