java.lang.AbstractMethodError: javax.xml.transform.TransformerFactory

java.lang.AbstractMethodError: javax.xml.transform.TransformerFactory

  • 1、环境及报错场景
  • 2、报错数据
  • 3、问题分析
  • 4、解决
  • 5、补充说明

 

1、环境及报错场景

  • jdk 1.7
  • idea
  • maven
  • html 转 pdf 时,报错
     

2、报错数据


2020-02-24 12:02:59.312 ERROR 9612 --- [nio-8080-exec-9] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [/ebid] threw exception [Handler dispatch failed; nested exception is java.lang.AbstractMethodError: javax.xml.transform.TransformerFactory.setFeature(Ljava/lang/String;Z)V] with root cause

java.lang.AbstractMethodError: javax.xml.transform.TransformerFactory.setFeature(Ljava/lang/String;Z)V
	at org.xhtmlrenderer.resource.XMLResource$IdentityTransformerPool.<init>(XMLResource.java:388)
	at org.xhtmlrenderer.resource.XMLResource$IdentityTransformerPool.<init>(XMLResource.java:396)
	at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.<init>(XMLResource.java:173)
	at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.<init>(XMLResource.java:170)
	at org.xhtmlrenderer.resource.XMLResource.<clinit>(XMLResource.java:67)
	at org.xhtmlrenderer.pdf.ITextRenderer.setDocumentFromString(ITextRenderer.java:171)
	at org.xhtmlrenderer.pdf.ITextRenderer.setDocumentFromString(ITextRenderer.java:166)
	at com.huajie.ebidtender.controller.pm.EtContractController.createPdf(EtContractController.java:463)
	at com.huajie.ebidtender.controller.pm.EtContractController$$FastClassBySpringCGLIB$$e90c054f.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:56)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:55)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
	at com.huajie.ebidtender.controller.pm.EtContractController$$EnhancerBySpringCGLIB$$155cf42c.createPdf(<generated>)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:665)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at com.huajie.ebidtender.filter.AuthFilter.doFilter(AuthFilter.java:215)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)
	
	
	
				  //System.setProperty("javax.xml.transform.TransformerFactory", "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");

 

3、问题分析

报错信息分析:

2020-02-24 12:02:59.312 ERROR 9612 --- [nio-8080-exec-9] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [/ebid] threw exception [Handler dispatch failed; nested exception is java.lang.AbstractMethodError: javax.xml.transform.TransformerFactory.setFeature(Ljava/lang/String;Z)V] with root cause

java.lang.AbstractMethodError: javax.xml.transform.TransformerFactory.setFeature(Ljava/lang/String;Z)V
	at org.xhtmlrenderer.resource.XMLResource$IdentityTransformerPool.<init>(XMLResource.java:388)
	at org.xhtmlrenderer.resource.XMLResource$IdentityTransformerPool.<init>(XMLResource.java:396)
	at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.<init>(XMLResource.java:173)
	at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.<init>(XMLResource.java:170)
	at org.xhtmlrenderer.resource.XMLResource.<clinit>(XMLResource.java:67)
	at org.xhtmlrenderer.pdf.ITextRenderer.setDocumentFromString(ITextRenderer.java:171)
	at org.xhtmlrenderer.pdf.ITextRenderer.setDocumentFromString(ITextRenderer.java:166)
	at com.huajie.ebidtender.controller.pm.EtContractController.createPdf(EtContractController.java:463)

 
对于 java.lang.AbstractMethodError 这种报错,根据经验首先怀疑 jar 直接引用或间接引用有冲突。
java.lang.AbstractMethodError: javax.xml.transform.TransformerFactory_第1张图片
关于 AbstractMethodError 的分析,详细可参考博主文章 Java AbstractMethodError 原因分析.
 
        这里是 org.xhtmlrenderer.resource.XMLResource 初始化失败,根据经验是间接引用的 jar冲突,导致多个 jar 中都有 javax.xml.transform.TransformerFactory 的路径,但是可能版本存在差异,没有javax.xml.transform.TransformerFactory.setFeature() 方法。

 
        由于项目pom文件中引用太多jar,找jar的引用冲突太复杂,尝试找了一下,还是放弃了,哈哈。
于是偷懒,利用 Java中System.setProperty() 和 System.getProperty() 方法来尝试解决一下。
 
于是在项目报错处添加以下代码 :
 
System.out.println(System.getProperty("javax.xml.transform.TransformerFactory"));
 
调用,然后打印出如下结果,发现返回为null;
java.lang.AbstractMethodError: javax.xml.transform.TransformerFactory_第2张图片

这个结果就很好说了,对象的都为空,肯定在调用对象的函数会报错。
 

4、解决

解决方案很简单粗暴,直接用 Java中System.setProperty() 方法,指定成为系统的全局变量!

在调用的前一句添加代码:
 
System.setProperty("javax.xml.transform.TransformerFactory", "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");

java.lang.AbstractMethodError: javax.xml.transform.TransformerFactory_第3张图片
 

5、补充说明

 

Java 中 System.setProperty() 用法
 

  • 设置指定键对值的系统属性
  • setProperty (String prop, String value);
     
  • 参数:
  • prop - 系统属性的名称。
  • value - 系统属性的值。
     
  • 返回:
  • 系统属性以前的值,如果没有以前的值,则返回 null。
     
  • 抛出:
  • SecurityException - 如果安全管理器存在并且其 checkPermission 方法不允许设置指定属性。
  • NullPointerException - 如果 key 或 value 为 null。
  • IllegalArgumentException - 如果 key 为空。
  • 注:这里的system,系统指的是 JRE (runtime)system,不是指 OS。

 
 
 
 
 
 
 
 .

你可能感兴趣的:(问题)