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