webservice簡單運行原理:
webservice的作用:解決異構軟件系統之間的通信。
基於Myeclipse開發webservice基於xfire的簡單實現
1)創建一個web工程
1:添加xfire的jar包
這個過程做了三個事情:
1:添加進來了兩個jar包
2:創建了一個webservice文件夾
3:在web.xml中添加了一個servlet
以上就是在服務器端做的三個事情
我們在服務器端要用webservice 要面向接口編程
所以我創建了一個接口和它的實現類
然後在services.xml配置我們的接口和實現類
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<!-- 名稱 -->
<name>hello</name>
<!-- 接口的路徑 -->
<serviceClass>cn.com.demo.hello.IHello</serviceClass>
<!-- 實現類的路徑 -->
<implementationClass>cn.com.demo.hello.impl.HelloImpl</implementationClass>
<!-- 服務的生命週期 -->
<scope>application</scope>
</service>
</beans>
做完上述步驟之後就驗證webservice是否發佈成功
1)將寫好的web工程加入到tomcat容器中
2)啓動tomcat容器
3)通過瀏覽器訪問web.xml中配置的servlet-ur
l
然後點擊wsdl就會看到
紅色字體部分就是實現類當中的方法
<xsd:element name="hello">就是方法名
<xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string"/>形參類型
<xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string"/>返回值類型
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>這個很重要 就是客戶端訪問uddi所需要的URL但是要
寫成這樣http://localhost:8080/WebServiceDemo/services/hello?wsdl
2)然後寫客戶端
1:首先創建一個java工程的客戶端不是web工程
@1:加入xfire jar包
這次要勾選上HTTP這個jar包
右鍵點擊src再創建一個webserviceClient
然後選擇Xfire:
然後下一步:
WSDL URL 就是前面用綠色字體寫的那個URL
在創建一個新的包名 點擊next 然後就產生如下幾個文件
這是helloClient的代碼
package cn.com.client;
import java.net.MalformedURLException;
import java.util.Collection;
import java.util.HashMap;
import javax.xml.namespace.QName;
import org.codehaus.xfire.XFireRuntimeException;
import org.codehaus.xfire.aegis.AegisBindingProvider;
import org.codehaus.xfire.annotations.AnnotationServiceFactory;
import org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.jaxb2.JaxbTypeRegistry;
import org.codehaus.xfire.service.Endpoint;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.soap.AbstractSoapBinding;
import org.codehaus.xfire.transport.TransportManager;
public class helloClient {
private static XFireProxyFactory proxyFactory = new XFireProxyFactory();
private HashMap endpoints = new HashMap();
private Service service0;
public helloClient() {
create0();
Endpoint helloPortTypeLocalEndpointEP = service0 .addEndpoint(new QName("http://hello.demo.com.cn", "helloPortTypeLocalEndpoint"), new QName("http://hello.demo.com.cn", "helloPortTypeLocalBinding"), "xfire.local://hello");
endpoints.put(new QName("http://hello.demo.com.cn", "helloPortTypeLocalEndpoint"), helloPortTypeLocalEndpointEP);
Endpoint helloHttpPortEP = service0 .addEndpoint(new QName("http://hello.demo.com.cn", "helloHttpPort"), new QName("http://hello.demo.com.cn", "helloHttpBinding"), "http://localhost:8080/WebServiceDemo/services/hello");
endpoints.put(new QName("http://hello.demo.com.cn", "helloHttpPort"), helloHttpPortEP);
}
public Object getEndpoint(Endpoint endpoint) {
try {
return proxyFactory.create((endpoint).getBinding(), (endpoint).getUrl());
} catch (MalformedURLException e) {
throw new XFireRuntimeException("Invalid URL", e);
}
}
public Object getEndpoint(QName name) {
Endpoint endpoint = ((Endpoint) endpoints.get((name)));
if ((endpoint) == null) {
throw new IllegalStateException("No such endpoint!");
}
return getEndpoint((endpoint));
}
public Collection getEndpoints() {
return endpoints.values();
}
private void create0() {
TransportManager tm = (org.codehaus.xfire.XFireFactory.newInstance().getXFire().getTransportManager());
HashMap props = new HashMap();
props.put("annotations.allow.interface", true);
AnnotationServiceFactory asf = new AnnotationServiceFactory(new Jsr181WebAnnotations(), tm, new AegisBindingProvider(new JaxbTypeRegistry()));
asf.setBindingCreationEnabled(false);
service0 = asf.create((cn.com.client.helloPortType.class), props);
{
AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://hello.demo.com.cn", "helloHttpBinding"), "http://schemas.xmlsoap.org/soap/http");
}
{
AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://hello.demo.com.cn", "helloPortTypeLocalBinding"), "urn:xfire:transport:local");
}
}
public helloPortType gethelloPortTypeLocalEndpoint() {
return ((helloPortType)(this).getEndpoint(new QName("http://hello.demo.com.cn", "helloPortTypeLocalEndpoint")));
}
public helloPortType gethelloPortTypeLocalEndpoint(String url) {
helloPortType var = gethelloPortTypeLocalEndpoint();
org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
return var;
}
public helloPortType gethelloHttpPort() {
return ((helloPortType)(this).getEndpoint(new QName("http://hello.demo.com.cn", "helloHttpPort")));
}
public helloPortType gethelloHttpPort(String url) {
helloPortType var = gethelloHttpPort();
org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
return var;
}
public static void main(String[] args) {
helloClient client = new helloClient();
helloPortType service = client.gethelloHttpPort();
String result = service.hello("zhangsan");
System.out.println("test client completed");
System.exit(0);
}
}
紅色字體很重要 創建一個helloClient 對象 通過這個對象來生成一個helloPortType對象來調用服務器的方法
這個就是webservice和不是web工程之間的通信
2)再做一個和web工程通信的列子此web工程集成了sutrts2框架
步驟和一般web工程一樣 添加該添加的jar 和wenservice集成的時候和前面不是web工程一樣關鍵是怎麼調用webservice中的方法
package cn.com.demo.action;
import cn.com.web.client.helloClient;
import cn.com.web.client.helloPortType;
public class HelloAction {
private String message;
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String hello(){
String result = "success";
helloClient client = new helloClient();
helloPortType hello = client.gethelloHttpPort();
message = hello.hello(userName);
return result;
}
}
看到這裏我們就發現了一個問題 就是不管怎麼用 都是要創建客戶端的實列 然後通過客戶端來訪問service的方法
今天就寫到這裏吧 實在是脖子太痛了 第一次寫技術博客 下次有時間在寫基於jax-ws的Demo
webservice的客戶端不只是能調用服務器的方法也可以調用實體對象 但是實體對象要進行序列化