与Spring引导devtools相关的推土机映射异常 [英] A dozer map exception related to Spring boot devtools
问题描述
我遇到了一个非常奇怪的例外,我不知道如何找到原因。
I have encountered a very strange exception, and I don't know how to find the reason.
商业背景:
添加商品,同时它是价格表,商品有差价水平用户的5个价格。
Business background: Add goods and meantime it's price list, a goods have 5 price for diff level user.
在控制器中,首先使用dozer将goodForm转换为商品,然后调用goodsService来保存商品。
在货物服务中保存货物,遍历货物价格表并填充货物ID到货物价格,
In controller, first convert goodForm to goods by using dozer, then call goodsService to save goods. In goodsService after saving goods, traversal goods price list and populate goodsId to goods price,
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());
但它抛出异常:
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]
此错误消息让我感到非常困惑。另外我写一个单元测试想要重复这个,但是失败了。
This error message let me feel very confused. In addition I write a unit test wanted to repeat this, but failed.
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)它失败了,但是在单元测试情况下它还可以吗?
Run this unit test, it executed ok. So why in real web situation(Spring boot + Jpa) it's failed, but in unit test situation it's ok?
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
If I generated a packaged jar, then execute this jar
java -jar target/myapp.jar
在这种情况下没有上述异常。
In this case without above exception.
我在pom.xml中评论了spring-boot-devtools,然后启动了应用程序,没有异常。
And I commented spring-boot-devtools in pom.xml, then started application, without above exception.
推荐答案
默认情况下,IDE中的任何打开项目都将使用restart类加载器和任何常规加载.jar文件将使用base类加载器加载。如果您处理多模块项目,而不是将每个模块导入IDE,则可能需要自定义项目。为此,您可以创建META-INF / spring-devtools.properties文件。
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.
spring-devtools.properties文件可以包含restart.exclude。并重新启动。包含。前缀属性。 include元素是应该上拉到restart类加载器中的项,而exclude元素是应该下推到base类加载器中的项。该属性的值是一个将应用于类路径的正则表达式模式。
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.
我的解决方案:put META-INF / spring-devtools.properties
内部资源文件夹,并添加此内容
My Solution: put META-INF/spring-devtools.properties
inside resources folder, and add this content
restart.include.dozer=/dozer-5.5.1.jar
这篇关于与Spring引导devtools相关的推土机映射异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!