Java calls WebService and axiswebservice through axis
In the morning, I gave me a task to connect to the webservice interfaces of other companies, various Baidu, and finally reached my mind. record it.
Jar package available, http://pan.baidu.com/s/1jSchC
Contains: axis. jar, commons-discovery.jar, commons-logging-1.0.4.jar, jaxrpc. jar, wsdl4j. jar, mail. jar.
Why is there mail. jar? I am not quite sure. If not, I will report a warning:
15:07:45 org. apache. axis. utils. JavaUtils isAttachmentSupported warning: Unable to find required classes (javax. activation. DataHandler and javax. mail. internet. MimeMultipart). Attachment support is disabled.
There are a lot of people talking about this problem on the Internet, but I still don't understand it. I still need to add mail. jar to fix it.
The webservice must have an interface address.
For example:
http://10.10.10.10:8080/xxxxx/services/XXXXWebservice?wsdl
Directly write the test code (I changed the package name, address, and class name to xxx)
Soapaction is obtained through the access interface address, targetNamespace
<wsdl:definitions targetNamespace="http://webservice.xxxx.com">
Package xx. xxxx. xxx; import java.net. URL; import javax. xml. namespace. QName; import javax. xml. rpc. parameterMode; import javax. xml. rpc. encoding. XMLType; import org. apache. axis. client. call; import org. apache. axis. client. service; public class XXXWebService {public static void main (String [] args) {String soapaction = "http://webservice.xxxxxx.com"; // your webservice address String endpoint = "http: // 10.10.10.10: 8080/xxxxx/services/XXXXWebservice "; Service service = new Service (); try {Call call = (Call) service. createCall (); call. setTimeout (new Integer (60000); call. setTargetEndpointAddress (new URL (endpoint); // call the method you need to call remotely. setOperationName (new QName (soapaction, "getXXXX"); // method parameter. If no parameter exists, ignore call. addParameter (new QName (soapaction, "xxxxxx"), XMLType. XSD_STRING, ParameterMode. IN); call. addParameter (new QName (soapaction, "xxxx"), XMLType. XSD_STRING, ParameterMode. IN); // set the return type. The json returned by the other interface is received using string, and the custom type is pasted with a code call. setReturnType (XMLType. XSD_STRING); // call the method and pass the parameter. If no parameter exists, call. invoke (new Object [] {null}); String result = (String) call. invoke (new Object [] {"xxxxx", "xx, xx"}); System. out. println (result);} catch (Exception e) {e. printStackTrace ();}}}
Below is the processing custom type processing custom return type I am using dom4j jar package: http://pan.baidu.com/s/1jGGfNZg
Call. setReturnType (XMLType. XSD_SCHEMA); // write a null Schema data = (Schema) call without parameters. invoke (new Object [] {null}); MessageElement [] datas = data. get_any (); for (int I = 0; I <datas. length; I ++) {SAXReader reader = new org. dom4j. io. SAXReader (); Document doc = reader. read (new ByteArrayInputStream (datas [I]. toString (). getBytes ();/* example: this is the document returned by your call Method * <ns: getXXXXXXX> * <ns: return type = "xx. xxx. xxx. xxxx. X XXXX "> * <ax21: objects type =" xx. xxx. xxx. xxxx. XXXXX "> * <ax21: attribute> barabara </ax21: attribute> * </ax21: objects> * </ns: return> * </ns: getXXXXXXX> * // The ns1: xxx corresponds to the ax21: objects Node = doc above. selectSingleNode ("ns1: xxx"); if (node! = Null) {// here is the Node attribute = node. selectSingleNode ("ns1: attribute"); System. out. println (attribute );}}
The above is all the content for calling webservice through axis. Now, it's just a bit of information, so I still need to learn more after work :)