1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| package clients;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
public class RESTClient {
private static String toEpr = "http://localhost:8080/axis2/services/MyService";
private static OMElement getPayload(String symbol) {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace("http://ws.apache.org/axis2", "tns");
OMElement method = fac.createOMElement("echo", omNs);
OMElement value = fac.createOMElement("Text", omNs);
value.addChild(fac.createOMText(value, symbol));
method.addChild(value);
return method;
}
public static void main(String[] args) {
try {
OMElement payload = getPayload("Hello Christophe !");
Options options = new Options();
options.setTo(new EndpointReference(toEpr));
options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
OMElement result = sender.sendReceive(payload);
XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out);
result.serialize(writer);
writer.flush();
} catch(AxisFault e) {
e.printStackTrace();
} catch(XMLStreamException e) {
e.printStackTrace();
} catch(FactoryConfigurationError e) {
e.printStackTrace();
}
}
} |
Partager