使用简单java类型构建webservice
示例代码如下:
service端
/**
* 使用简单的java类型构建webservice
*
* @time 下午6:45:28
* @author retacn yue
* @Email
[email protected]
*/
public class HelloJava {
public String sayHello(String name) throws XMLStreamException {
StringBuffer sb = new StringBuffer();
sb.append("hello ");
sb.append(name);
return sb.toString();
}
}
<service name="HelloJava" >
<parameter name="ServiceClass"
locked="false">cn.yue.axis2service.jstyle.HelloJava</parameter>
<messageReceivers>
<messageReceiver
mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>
<actionMapping>urn:sayHello</actionMapping>
</service>
客户端
/**
* 阻塞回调
*
* @time 下午3:27:21
* @author retacn yue
* @Email
[email protected]
*/
public class HelloJavaNonBlockCB implements AxisCallback {
private boolean complete = false;
@Override
public void onMessage(MessageContext msgContext) {
System.out.println(msgContext.getEnvelope().getBody());
OMElement element = msgContext.getEnvelope().getBody
().getFirstElement();
//此处第二个参数用return
OMElement result = element.getFirstChildWithName(new QName
("http://jstyle.axis2service.yue.cn", "return"));
System.out.println(result.getText());
synchronized (this) {
this.notify();
}
}
@Override
public void onFault(MessageContext msgContext) {
System.out.println(msgContext.getEnvelope().getBody().getFault
().toString());
synchronized (this) {
this.notify();
}
}
@Override
public void onError(Exception e) {
e.printStackTrace();
synchronized (this) {
this.notify();
}
}
@Override
public void onComplete() {
this.complete = true;
synchronized (this) {
this.notify();
}
}
public boolean isComplete() {
return complete;
}
}
**
* service端用java的简单类型
*
* 非阻塞(异步)全双工
*
* @time 下午4:04:55
* @author retacn yue
* @Email
[email protected]
*/
public class HelloJavaDualNonBlock {
private static EndpointReference endpointReference = new EndpointReference
("http://localhost:8089/services/HelloJava");
public void sayHello() {
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace namespace = factory.createOMNamespace
("http://jstyle.axis2service.yue.cn", "");
OMElement method = factory.createOMElement("sayHello", namespace);
OMElement name = factory.createOMElement("name", namespace);
name.setText("yuezhenhua");
method.addChild(name);
method.build();
Options options = new Options();
options.setTo(endpointReference);
// 设置客户端为双工全开
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
// 在客户端打开一个监听
options.setUseSeparateListener(true);
options.setAction("urn:sayHello");
ServiceClient sender = null;
HelloJavaNonBlockCB blockCB = new HelloJavaNonBlockCB();
try {
sender = new ServiceClient();
// 寻址
sender.engageModule(Constants.MODULE_ADDRESSING);
sender.setOptions(options);
sender.sendReceiveNonBlocking(method, blockCB);
synchronized (blockCB) {
try {
blockCB.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (AxisFault e) {
e.printStackTrace();
} finally {
try {
if (null != sender) {
sender.cleanup();
}
} catch (AxisFault e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
HelloJavaDualNonBlock client = new HelloJavaDualNonBlock();
client.sayHello();
}
}
soap ui生成的客户端
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:jst="http://jstyle.axis2service.yue.cn">
<soapenv:Header/>
<soapenv:Body>
<jst:sayHello>
<!--Optional:-->
<jst:name>yuezhenhua</jst:name>
</jst:sayHello>
</soapenv:Body>
</soapenv:Envelope>
java 代码实现
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace namespace = factory.createOMNamespace
("http://jstyle.axis2service.yue.cn", "");
OMElement method = factory.createOMElement("sayHello", namespace);
OMElement name = factory.createOMElement("name", namespace);
name.setText("yuezhenhua");
method.addChild(name);
method.build();
复杂类型的webservcie
service端
示例代码如下:
/**
*
* @time 下午7:50:05
* @author retacn yue
* @Email
[email protected]
*/
public class Person implements Serializable, Cloneable {
private static final long serialVersionUID = 1L;
private String name = "";
private String age = "";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
protected Object clone() {
Object object = null;
try {
object = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
object = null;
}
return object;
}
}
/**
* java复杂类型
*
*
*
* @time 下午7:53:21
* @author retacn yue
* @Email
[email protected]
*/
public class PersonService {
public OMElement getPersons(OMElement person) {
Person person2 = new Person();
List<Person> persons = new ArrayList<Person>();
person2.setAge("28");
person2.setName("yuezhenhua");
persons.add((Person) person2.clone());
person2.setAge("22");
person2.setName("zhansan");
persons.add((Person) person2.clone());
OMElement element = BeanUtil.getOMElement(new QName("root"),
persons.toArray(), new QName("person"), false, null);
return element;
}
}
<service name="PersonService" >
<parameter name="ServiceClass"
>cn.yue.axis2service.javacomplextype.PersonService</parameter>
<operation name="getPersons">
<actionMapping>urn:getPersons</actionMapping>
<messageReceiver
class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
</service>
客户端
/**
*
* @time 下午7:50:05
* @author retacn yue
* @Email
[email protected]
*/
public class Person implements Serializable, Cloneable {
private static final long serialVersionUID = 1L;
private String name = "";
private String age = "";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
protected Object clone() {
Object object = null;
try {
object = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
object = null;
}
return object;
}
}
/**
* 阻塞回调
*
* @time 下午3:27:21
* @author retacn yue
* @Email
[email protected]
*/
public class GetPersonNonBlockCB implements AxisCallback {
@Override
public void onMessage(MessageContext msgContext) {
OMElement result = msgContext.getEnvelope().getBody
().getFirstElement();
System.out.println("result=" + result);
if (null == result) {
return;
}
@SuppressWarnings("rawtypes")
Iterator iterator = result.getChildElements();
while (iterator.hasNext()) {
OMNode omNode = (OMNode) iterator.next();
if (omNode.getType() == OMNode.ELEMENT_NODE) {
OMElement element = (OMElement) omNode;
if (element.getLocalName().toLowerCase().equals
("person")) {
try {
Person person = (Person)
BeanUtil.processObject(element, Person.class, null, true, new DefaultObjectSupplier
(), null);
System.out.println("person name=" +
person.getName() + " \n person age=" + person.getAge());
} catch (AxisFault e) {
e.printStackTrace();
}
}
}
}
synchronized (this) {
this.notify();
}
}
@Override
public void onFault(MessageContext msgContext) {
System.out.println(msgContext.getEnvelope().getBody().getFault
().toString());
synchronized (this) {
this.notify();
}
}
@Override
public void onError(Exception e) {
e.printStackTrace();
synchronized (this) {
this.notify();
}
}
@Override
public void onComplete() {
synchronized (this) {
this.notify();
}
}
}
/**
* service端用java的简单类型
*
* 非阻塞(异步)全双工
*
* @time 下午4:04:55
* @author retacn yue
* @Email
[email protected]
*/
public class GetPersonDualNonBlock {
private static EndpointReference endpointReference = new EndpointReference
("http://localhost:8089/services/PersonService");
public void getPersons() {
Options options = new Options();
options.setTo(endpointReference);
options.setAction("urn:getPersons");
// 设置客户端为双工全开
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
// 在客户端打开一个监听
options.setUseSeparateListener(true);
ServiceClient sender = null;
GetPersonNonBlockCB blockCB = new GetPersonNonBlockCB();
try {
sender = new ServiceClient();
sender.setOptions(options);
// 寻址
sender.engageModule(Constants.MODULE_ADDRESSING);
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace namespace = factory.createOMNamespace
("http://javacomplextype.axis2service.yue.cn", "");
OMElement method = factory.createOMElement("getPersons",
namespace);
OMElement person = factory.createOMElement("person",
namespace);
person.setText("");
method.addChild(person);
sender.sendReceiveNonBlocking(method, blockCB);
synchronized (blockCB) {
try {
blockCB.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (AxisFault e) {
e.printStackTrace();
} finally {
try {
if (null != sender) {
sender.cleanup();
}
} catch (AxisFault e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
GetPersonDualNonBlock client = new GetPersonDualNonBlock();
client.getPersons();
}
}