在现有项目中使用axis2配置webService

1.首先到官方网站下载文档及axis2.war

http://ws.apache.org/axis2


2.解包axis2.war

将WEB-INF\lib中的jar包都拷到当前项目下的lib中,将WEB-INF\conf中的axis2.xml拷贝到当前项目路径中,注意注释掉关于module方面的代码,在当前项目的web.xml中添加

<servlet>
<display-name>Apache-Axis Servlet</display-name>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/ *</url-pattern>
</servlet-mapping>


3.service的一个例子

本例子是从客户端获得一个OMElement,解析后将当前时间写到xml文件中,封装成OMElement后再返回给客户端

RevokeService.java


package com.zotn.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Iterator;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;


public class RevokeService {


public static OMElement requestSoap = null;


private static void writeResponse(String res, String filePath) {
try {
FileOutputStream fos = new FileOutputStream(filePath);
byte[] bytes = res.getBytes();
fos.write(bytes);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


private static String makePath(String fileName) {
String path =
"d:/" + fileName;
return path;
}
//主要的WEB服务方法
public OMElement RevokeCertRequest(OMElement soapBody) {
requestSoap = soapBody;
QName issuerName = new QName("Issuer");
QName serialName = new QName("Serial");
QName revocationDateName = new QName("RevocationDate");
Iterator it = requestSoap.getChildElements();
OMElement issuerElement = (OMElement)it.next();
OMElement serialElement = (OMElement)it.next();
OMElement revocationDateElement = (OMElement)it.next();
String issuer = issuerElement.getText();
String serial = serialElement.getText();
String revocationDate = revocationDateElement.getText();

// print out the value
System.out.println(issuer);
System.out.println(serial);
System.out.println(revocationDate);

// TODO use "issuer,serial,revocationDate" to do business

// Generate the soap response message
OMFactory soapFactory = OMAbstractFactory.getOMFactory();
OMNamespace omNs = soapFactory.createOMNamespace(
"http://www.sdes.net/", "");
OMElement soapResponse = soapFactory.createOMElement(
"RevokeCertResponse", omNs);
OMElement soapMain = soapFactory.createOMElement("RevokeDate", omNs);
//soapMain.setText(revocationDate);
Calendar cal = Calendar.getInstance();
String time = cal.getTime().toLocaleString();
soapMain.setText(time);
soapResponse.addChild(soapMain);
soapResponse.build();
String path = makePath("resMsg.xml");
writeResponse(soapResponse.toString(), path);
return soapResponse;
}
}

4.在当前项目路径下增加services\TodoService\META-INF\services.xml文件和ServiceData.xml文件,内容分别为

services.xml


<?xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
<service name="CertRevokeService">
<description>
This is the service for revoking certificate.
</description>
<parameter name="ServiceClass" locked="false">
com.zotn.util.RevokeService
</parameter>
<operation name="RevokeCertRequest">
<messageReceiver
class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" />
<actionMapping>urn:RevokeCertRequest</actionMapping>
</operation>
</service>
</serviceGroup>


ServiceData.xml


<?xml version="1.0" encoding="UTF-8"?>
<ServiceData>
<Data dialect="http://schemas.xmlsoap.org/wsdl/"
file="META-INF/DefaultAxis2DataLocatorDemoService.wsdl">
<URL>"http://localhost/services/CertRevokeService?wsdl" </URL>
</Data>
<Data dialect ="http://www.w3.org/2001/XMLSchema " file="META-INF/DefaultAxis2DataLocatorDemoService.xsd">
<URL> http://localhost/services/CertRevokeService?xsd </URL>
</Data>
</ServiceData>


5.启动应用服务器,访问http://localhost/services/CertRevokeService?wsdl,看是否能看到wsdl信息,如果能看到,说明服务发布成功。


6.客户端例子

客户端使用AXIOM封装了一个xml消息,并将这个消息发送到服务端,服务端返回一个封装有当前时间的xml消息,实现了数据的交互。

package test;

import java.util.Iterator;

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.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;


public class TestCertRevokeService {

private static EndpointReference targetEPR = new EndpointReference(
"http://localhost/services/CertRevokeService");

public static OMElement getSoapRequestMessage() {
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace omNs =
factory.createOMNamespace("http://www.sdes.net", "");
OMElement issuer = factory.createOMElement("Issuer", omNs);
OMElement serial = factory.createOMElement("Serial", omNs);
OMElement revocationDate =
factory.createOMElement("RevocationDate",
omNs);
issuer.setText("C=JP,O=FX,CN=SDES CA");
serial.setText("1234567890");
revocationDate.setText("2007-01-01T00:00:00.000Z");

OMElement requestSoapMessage = factory.createOMElement(
"RevokeCertRequest", omNs);
requestSoapMessage.addChild(issuer);
requestSoapMessage.addChild(serial);
requestSoapMessage.addChild(revocationDate);
requestSoapMessage.build();
return requestSoapMessage;
}


public static void main(String[] args) {
OMElement requestSoapMessage = getSoapRequestMessage();
System.out.println(" getSoapRequestMessage Succuss !");
Options options = new Options();
options.setTo(targetEPR);
System.out.println(" options setTo Succuss !");
ServiceClient sender = null;
try {
sender = new ServiceClient();
sender.setOptions(options);
System.out.println(" setOptions Succuss !");
OMElement responseSOAP = sender.sendReceive(requestSoapMessage);
Iterator it = responseSOAP.getChildElements();
OMElement RevokeDate = (OMElement) it.next();
String date = RevokeDate.getText();
System.out.println("----"+date+"----");
} catch (AxisFault e) {
System.out.println(e.getFaultCode().toString());
}
}
}
总体来说当前项目上的webService调用已经实现了,至于更多功能等研究后再添加了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值