最近蛮多人在问这方面问题的,我把之间的回答再贴一下,楼主可以参考一下:
建议用Axis2来实现WebServices,流程如下(我用的是Axis2项目里的用户手册的例子):
服务器端:
1、 public class MyService{
public void ping(OMElement element){
//这里是你在服务器端要实现的功能代码
} } 2、建立service.xml,放在以上java文件同路径下的META-INF文件夹下
<service >
<description>
This is a sample Web Service with two operations, echo and ping.
</description>
<parameter name="ServiceClass" locked="false"> userguide.example1.MyService </parameter>
<operation name="ping">
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver"/>
<actionMapping> urn:ping </actionMapping>
</operation>
</service>
3、将上面2个文件打成jar文件,可以考虑用eclipse的导出jar功能来实现,比较方便,在将这个jar文件的后缀名改成aar,比如test.aar,将这个文件放在你那个struts项目的WEB-INF\services下,这样你启动你的项目时候,WebServices的服务器端就可以发布成功了。检查是否发布成功的方法是,用浏览器访问:http://localhost:8080/muProject/services/aaa.wsdl,其中aaa的名字是service.xml中配置的。 现在讲下客户端的编写,举个最简单的例子吧,
客户端很简单,如下: package userguide.clients;import org.apache.axiom.om.OMElement;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;/**
* Sample for synchronous single channel blocking service invocation.
* Message Exchage Pattern IN-OUT
*/
public class EchoBlockingClient {
private static EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/MyService"); public static void main(String[] args) {
try {
OMElement payload = ClientUtil.getEchoOMElement();
Options options = new Options();
options.setTo(targetEPR);
options.setAction("urn:ping"); //Blocking invocation
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
OMElement result = sender.sendReceive(payload); System.out.println(result); } catch (AxisFault axisFault) {
axisFault.printStackTrace();
}
}
}以上就算完成了一个简单的用Axis2来创建WebServices的过程,具体的所用到的项目包,用户手册等资料,在apache的axis项目页上都可以找到
http://ws.apache.org/axis2/
建议用Axis2来实现WebServices,流程如下(我用的是Axis2项目里的用户手册的例子):
服务器端:
1、 public class MyService{
public void ping(OMElement element){
//这里是你在服务器端要实现的功能代码
} } 2、建立service.xml,放在以上java文件同路径下的META-INF文件夹下
<service >
<description>
This is a sample Web Service with two operations, echo and ping.
</description>
<parameter name="ServiceClass" locked="false"> userguide.example1.MyService </parameter>
<operation name="ping">
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver"/>
<actionMapping> urn:ping </actionMapping>
</operation>
</service>
3、将上面2个文件打成jar文件,可以考虑用eclipse的导出jar功能来实现,比较方便,在将这个jar文件的后缀名改成aar,比如test.aar,将这个文件放在你那个struts项目的WEB-INF\services下,这样你启动你的项目时候,WebServices的服务器端就可以发布成功了。检查是否发布成功的方法是,用浏览器访问:http://localhost:8080/muProject/services/aaa.wsdl,其中aaa的名字是service.xml中配置的。 现在讲下客户端的编写,举个最简单的例子吧,
客户端很简单,如下: package userguide.clients;import org.apache.axiom.om.OMElement;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;/**
* Sample for synchronous single channel blocking service invocation.
* Message Exchage Pattern IN-OUT
*/
public class EchoBlockingClient {
private static EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/MyService"); public static void main(String[] args) {
try {
OMElement payload = ClientUtil.getEchoOMElement();
Options options = new Options();
options.setTo(targetEPR);
options.setAction("urn:ping"); //Blocking invocation
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
OMElement result = sender.sendReceive(payload); System.out.println(result); } catch (AxisFault axisFault) {
axisFault.printStackTrace();
}
}
}以上就算完成了一个简单的用Axis2来创建WebServices的过程,具体的所用到的项目包,用户手册等资料,在apache的axis项目页上都可以找到
http://ws.apache.org/axis2/
解决方案 »
- SSH中,一个action中,放两个service,这样做对于设计模式好么?
- 关于poi导入的问题
- Axis的Http post方式交互
- TOMCAT关闭时如何关闭HIBERNAET连接池???
- 请问是什么问题,请大家看看咯!谢谢!
- 一个超级简单的struts,Action1中的转向为什么都是"success"呢?
- 求教:数据库插件该怎么做?
- CVS Lock问题
- struts怎么从一个框架集(frameSet)跳转到一个单一页面?(就象smth.org)
- 急!怎么样Tomcat的jsp页面显示中文?
- xp系统为什么不识别以jsp后缀名的文件呢?
- [向 大家 请教 ] 分不够提问 就90分!!! !
请高手指点,JAVA发布成WEB SERVICE我真的很弱,谢谢。