WebService 简单应用

本文介绍使用 XFire 框架实现 WebService 的过程,包括服务器端搭建、客户端调用等步骤,并验证了服务的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.编写服务器端

1.在myeclipes中新建一个webservive项目mywebservice

2.配置web.xml
   
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

3.新建接口类Helloworldservice

package com.web.service;

public interface Helloworldservice {
public String sayHellow(String str);
}

4.新建实现类Helloworldservice

package com.web.impl;

import com.web.service.Helloworldservice;

public class Helloworldimpl implements Helloworldservice {

public String sayHellow(String str) {
return "您好,"+str+"!";
}

}

5.配置services.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<!--服务名称-->
<name>hellow</name>
<namespace>gjy</namespace>
<!--服务接口-->
<serviceClass>com.gjy.inter.Helloworldservice</serviceClass>
<!--服务实现类-->
<implementationClass>com.gjy.impl.Helloworldimpl</implementationClass>
</service>
</beans>


至此服务器端编写完毕

在tomcat中部署mywebservice,启动tomcat,在浏览器中输入http://localhost:8088/mywebservice/services/hellow?wsdl,回车,出现以下内容,证明服务器端编写成功

<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions targetNamespace="gjy" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:tns="gjy" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
- <wsdl:types>
- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="gjy">
- <xsd:element name="sayHellow">
- <xsd:complexType>
- <xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
- <xsd:element name="sayHellowResponse">
- <xsd:complexType>
- <xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
- <wsdl:message name="sayHellowRequest">
<wsdl:part name="parameters" element="tns:sayHellow" />
</wsdl:message>
- <wsdl:message name="sayHellowResponse">
<wsdl:part name="parameters" element="tns:sayHellowResponse" />
</wsdl:message>
- <wsdl:portType name="hellowPortType">
- <wsdl:operation name="sayHellow">
<wsdl:input name="sayHellowRequest" message="tns:sayHellowRequest" />
<wsdl:output name="sayHellowResponse" message="tns:sayHellowResponse" />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="hellowHttpBinding" type="tns:hellowPortType">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="sayHellow">
<wsdlsoap:operation soapAction="" />
- <wsdl:input name="sayHellowRequest">
<wsdlsoap:body use="literal" />
</wsdl:input>
- <wsdl:output name="sayHellowResponse">
<wsdlsoap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="hellow">
- <wsdl:port name="hellowHttpPort" binding="tns:hellowHttpBinding">
<wsdlsoap:address location="http://localhost:8088/mywebservice/services/hellow" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>


二.客户端编写

1.新建一个普通的java工程WebwerviceClient,引入XFire的jar包


2.编写客户端代码


package com.web.client;

import java.net.MalformedURLException;
import java.net.URL;

import org.codehaus.xfire.client.Client;

public class HelloworldClient {
public static void main(String args[]){
try {
Client client=new Client(new URL("http://localhost:8088/mywebservice/services/hellow?wsdl"));
Object[] result=client.invoke("sayHellow", new Object[]{"龚先生"});
System.out.println(result[0]);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

}
}


启动服务端,运行客户端,出现结果: 您好,龚先生!

至此一个简单的webservice应用开发完毕.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值