Web 服务返回 java.lang.reflect.InvocationTargetException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3000550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:27:00  来源:igfitidea点击:

Webservice returns java.lang.reflect.InvocationTargetException

javaweb-services

提问by Damian

I am receiving the above message when making a request to a java webservice.

我在向 Java 网络服务发出请求时收到上述消息。

We originally created a Java Console application and manually submitted an xml file. When running this as a Java Application the response is successfully created and displayed by using System.out.println. We are creating the web service by selecting the java file that contains the methods and choosing "create webservice" specifying the dynamic project that the webservice is to be created in and the methods to be exposed.

我们最初创建了一个 Java Console 应用程序并手动提交了一个 xml 文件。将此作为 Java 应用程序运行时,使用 System.out.println 成功创建和显示响应。我们通过选择包含方法的 java 文件并选择“create webservice”来指定要在其中创建 Web 服务的动态项目和要公开的方法来创建 Web 服务。

What the application is doing is taking an xml file and unmarshalling this to an object using:

应用程序正在做的是获取一个 xml 文件并使用以下方法将其解组到一个对象:

 public static Object unmarshalToObject(Class classToBeBound,
   String xmlRequest) {
  Object obj = new Object();
  try {
   JAXBContext jc = JAXBContext.newInstance(classToBeBound);
   Unmarshaller um = jc.createUnmarshaller();
   obj = um.unmarshal(new StringReader(xmlRequest));
  } catch (Exception e) {
   e.printStackTrace()
  }
  return obj;
 }

Some processing is carried out on the file and then an object is marshalled to xml as follows:

对文件进行一些处理,然后将对象编组为 xml,如下所示:

 public static String marshalToXML(Object data) {
  StringWriter sw = new StringWriter();
  try {
   logger.info("Create new Marshall");
   JAXBContext jc = JAXBContext.newInstance("ContextPathName");
   logger.info("Marshalled to xmlObjects");
   Marshaller marshaller = jc.createMarshaller();
   marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
   marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
   marshaller.marshal(data, sw);
  } catch (Exception e) {
   logException(logger, e);
  }
  return sw.toString();
 }

The following is the line of code that seems to be causing an issue as the logger displays the message prior to this:

以下是似乎导致问题的代码行,因为记录器在此之前显示消息:

JAXBContext jc = JAXBContext.newInstance("ContextPathName");

The webservice never gets to the next line - the following is the body of the SOAP message:

Web 服务永远不会到达下一行 - 以下是 SOAP 消息的正文:

  <soapenv:Fault>
     <faultcode>soapenv:Server.userException</faultcode>
     <faultstring>java.lang.reflect.InvocationTargetException</faultstring>
     <detail>
        <ns1:hostname xmlns:ns1="http://xml.apache.org/axis/">servername</ns1:hostname>
     </detail>
  </soapenv:Fault>

I have added Try/Catch around this section of code even as far as looking for JAXBExceptions but this does not seem to catch anything - nor does the general exception.

我在这部分代码周围添加了 Try/Catch 甚至就寻找 JAXBExceptions 而言,但这似乎没有捕获任何东西 - 一般异常也没有。

This issue does not occur when running the console application. The build path for this includes the following contents of sun\jwsdp-2.0\jaxb\lib:

运行控制台应用程序时不会发生此问题。这个的构建路径包括 sun\jwsdp-2.0\jaxb\lib 的以下内容:

jaxb-api.jar
jsr173_1.0_api.jar
jaxb-impl.jar

I have added these to the lib folder in the WEB-INF file of the dynamic project.

我已将这些添加到动态项目的 WEB-INF 文件中的 lib 文件夹中。

I am running the webservice in JBuilder 2008 R2 and using SOAPUI to submit the request - this points to the wsdl generated when creating the webservice.

我在 JBuilder 2008 R2 中运行 webservice 并使用 SOAPUI 提交请求 - 这指向创建 webservice 时生成的 wsdl。

If anyone has any help or ideas on how to solve this could they please reply - thanks for taking the time to read this post!

如果有人对如何解决此问题有任何帮助或想法,请回复 - 感谢您花时间阅读这篇文章!

回答by Rónán ó Braonáin

I am doing quite similar to you, taking in some XML via SOAP and marshalling it into objects. This isn't necessarily related to the problem.

我的做法与您非常相似,通过 SOAP 接收一些 XML 并将其编组为对象。这不一定与问题有关。

The InvocationException is thrown when there is a problem with running some Java code inside of Axis in processing the request. You need to specify some extra options to get access to log output from Axis itself. Once you have this output, you will more than likely see an exception that will make sense to you (in my case I was using a class which I hadn't included on my server classpath).

当在处理请求时在 Axis 内部运行某些 Java 代码出现问题时,会抛出 InvocationException。您需要指定一些额外的选项才能访问 Axis 本身的日志输出。一旦你有了这个输出,你很可能会看到一个对你有意义的异常(在我的例子中,我使用了一个我没有包含在我的服务器类路径中的类)。

Basically you need to add a LogHandler to your WSDD file. The following page has a good howto on what you need in the WSDD. However this page talks about the client-config.wsdd for an Axis client, whereas I am bundling Axis in my EAR and made the changes to server-config.wsdd.

基本上,您需要将 LogHandler 添加到您的 WSDD 文件中。以下页面提供了有关您在 WSDD 中所需内容的好方法。但是,此页面讨论了 Axis 客户端的 client-config.wsdd,而我将 Axis 捆绑在我的 EAR 中并对 server-config.wsdd 进行了更改。

http://www.theserverside.com/discussions/thread.tss?thread_id=35765

http://www.theserverside.com/discussions/thread.tss?thread_id=35765

This pagehas info more specific to Tomcat

此页面包含更特定于 Tomcat 的信息

Note that the save location of axis.log can vary depending on your server and OS. Some people report it appearing in the System32 directory of Windows, the 2nd link says it will appear in the bin/ directory in Tomcat. In my case (Mac OS X, Glassfish 2.1.1) it turned up in the config/ directory of my domain.

请注意,axis.log 的保存位置可能因您的服务器和操作系统而异。有人报告它出现在Windows的System32目录中,第二个链接说它会出现在Tomcat的bin/目录中。在我的情况下(Mac OS X,Glassfish 2.1.1)它出现在我的域的 config/ 目录中。

回答by Light Yagami

I've recently experienced this issue. This occurs when we are not able to find relevant java code on the server we point to.

我最近遇到了这个问题。当我们无法在我们指向的服务器上找到相关的 java 代码时,就会发生这种情况。

Here are a few possibilities that you could look into :

以下是您可以研究的几种可能性:

?The right JAR file does not exist on the server

? 服务器上不存在正确的 JAR 文件

?The WSDL and the subsequent Jar file on the Server has been updated and the WSDLconsumed is NOT the latest

?服务器上的 WSDL 和后续 Jar 文件已更新,并且 WSDLconsumed 不是最新的

?There are multiple jar files of the same type in the JAR folder. In most cases users rename their old JAR files before placing the newly updated JAR files. This should NOT be done as the right methods are not picked up. So get rid of ALL unnecessary JAR files from the SERVER. see below

? JAR 文件夹中有多个相同类型的 jar 文件。在大多数情况下,用户在放置新更新的 JAR 文件之前重命名他们的旧 JAR 文件。这不应该做,因为没有选择正确的方法。所以从服务器中删除所有不必要的 JAR 文件。见下文