Webservice returns java.lang.reflect.InvocationTargetException

给你一囗甜甜゛ 提交于 2021-02-19 05:16:27

问题


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

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.

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

 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:

 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:

  <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.

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:

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.

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.

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!


回答1:


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.

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).

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.

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

This page has info more specific to 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.




回答2:


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

Here are a few possibilities that you could look into :

•The right JAR file does not exist on the server

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

•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



来源:https://stackoverflow.com/questions/3000550/webservice-returns-java-lang-reflect-invocationtargetexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!