The application of Axis WebService

Source: Internet
Author: User

Import Java.io.StringReader;
Import Java.io.StringWriter;


Import Javax.xml.bind.JAXBContext;
Import javax.xml.bind.JAXBException;
Import Javax.xml.bind.Marshaller;
Import Javax.xml.bind.Unmarshaller;
Import Javax.xml.namespace.QName;


Import Org.apache.axis2.AxisFault;
Import org.apache.axis2.addressing.EndpointReference;
Import org.apache.axis2.client.Options;
Import org.apache.axis2.rpc.client.RPCServiceClient;
Import Org.junit.Test;
Import Com.name.pojo.son.Res;
Import Com.name.pojo.Req;


public class Testsxws {


@SuppressWarnings ("Static-access")
Bean to XML
@Test
public void Testwsres () {
StringWriter SW = new StringWriter ();
Req Req = new Req ("code", "111111111111", "" "," Class 1 "," 192.168.1.148 "," 2014-08-11 ");
try {
Jaxbcontext context = jaxbcontext.newinstance (Req.class);
Marshaller Marshaller = Context.createmarshaller ();
Marshaller.setproperty (Marshaller. Jaxb_encoding, "GBK");
Marshaller.marshal (req, SW);
SYSTEM.OUT.PRINTLN ("-----encapsulated XML-------" +sw.tostring ());
} catch (Jaxbexception e) {
E.printstacktrace ();
}
}


Tune WebService
@Test
public void Testsxws () {
try {
Rpcserviceclient is called by RPC mode
Rpcserviceclient client = new Rpcserviceclient ();
Options options = Client.getoptions ();
Set the URL of the call WebService
String address = "Http://192.168.1.16/services/HndyService";
EndpointReference EPF = new EndpointReference (address);
Options.setto (EPF);

/**
*
* QName namespace parameter is http://axis2.com, HelloWorldService service class package name upside down can be
* Hello is the method name
*/
QName QName = new QName ("http://service.yz.zyz.modules.whcss.com", "Insertsxinfo");
Specify the method to invoke and pass the parameter data, and set the type of the return value
Object[] result = client.invokeblocking (QName, new object[] {REQXML1 ()},
New class[] {string[].class});
string[] obj = (string[]) result[0];

SYSTEM.OUT.PRINTLN ("---sent over the XML----" +obj[0]);

try {
Jaxbcontext context = jaxbcontext.newinstance (Res.class);
Unmarshaller Unmarshaller = Context.createunmarshaller ();
Res u = (res) Unmarshaller.unmarshal (new StringReader (obj[0]));
Back to something
System.out.println ("Status:" +u.getdata (). getzt () + "," +u.getresult (). GetMessage () + "," +u.getresult (). GetCode ());
} catch (Jaxbexception e) {
E.printstacktrace ();
}

} catch (Axisfault Axisfault) {
Axisfault.printstacktrace ();
}

}

-Sealed XML
@SuppressWarnings ("Static-access")
private static String ReqXml1 () {
StringWriter SW = new StringWriter ();
Req Req = new Req ("code", "2222222222", "1", "Category 1", "192.168.1.18", "2014-08-11");
try {
Jaxbcontext context = jaxbcontext.newinstance (Req.class);
Marshaller Marshaller = Context.createmarshaller ();
Marshaller.setproperty (Marshaller. Jaxb_encoding, "GBK");//Set Pass XML encoding
Marshaller.marshal (req, SW);
SYSTEM.OUT.PRINTLN ("-----encapsulated XML-------" +sw.tostring ());
} catch (Jaxbexception e) {
E.printstacktrace ();
}
return sw.tostring ();
}


}

The application of axis webservice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.