當前位置:首頁 » 編程語言 » webservice實例java

webservice實例java

發布時間: 2024-04-15 14:56:07

❶ 用java怎麼寫webservice

Web Services以XML作為數據交換的標准格式,它是跨平台的應用,允許以任何方式創建Web Services,在.NET、Java平台上訪問
在Java平台創建和訪問Web Service多通過Axis完成。Axis本質上就是一個SOAP引擎,提供創建伺服器端、客戶端和網關SOAP操作的基本框架。Axis目前版本是為Java編寫的。在使用Axis訪問Web Service時,需要引入以下包(10個):axis-ant.jar、axis.jar、commons-discovery-0.2.jar、commons-logging-1.0.4.jar、jaxrpc.jar、log4j-1.2.8.jar、saaj.jar、wsdl4j-1.5.1.jar、activation-1.1.jar和mail-1.4.jar。
(1)訪問Java創建的Web Service
在當前Java客戶端應用中添加相應的10個Axis包,編寫客戶端程序:
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class Test {
public static void main(String[] args) throws Exception {
try{
String endpoint = "http://localhost:8080/MyService/services/Hello";
Service service = new Service();
Call call = (Call)service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName("getHello");
String res = (String) call.invoke(new Object[]{});
System.out.println(res);
}
catch (Exception ex){
ex.printStackTrace();
}
}
}
其中兩處代碼加粗,第一處表示引用Java Web Service的URL,第二處表示公共的方法名稱。

❷ java調用webservice例子

現在大多數項目都會用到spring,所以選擇 CXF 框架,cxf能很好的和spring結合


在官網下載最新版 xcf 3.0.3 網站 http://cxf.apache.org/


MyEclipse項目結構圖


結構圖中各個文件源碼

HelloWorldImpl.java

---------

import javax.jws.WebService;


@WebService(endpointInterface = "IHelloWorld", serviceName = "HelloWorld")

public class HelloWorldImpl implements IHelloWorld {

@Override

public String sayHello(String text) {

return "serviceSay: " + text;

}

}

------------------------------------------------------------------------------------------------



IHelloWorld.java

---------

import javax.jws.WebService;


@WebService

public interface IHelloWorld {

public String sayHello(String text);

}

------------------------------------------------------------------------------------------------


Test.java

---------

import org.apache.cxf.endpoint.Client;

import org.apache.cxf.endpoint.dynamic.DynamicClientFactory;


public class Test {

public static void main(String[] args) throws Exception {

DynamicClientFactory dcf = DynamicClientFactory.newInstance();

Client c = dcf.createClient("http://localhost:8080/cxf/ws/hwUrl?wsdl");

Object[] param = new Object[] { "----test....." };

Object[] result = c.invoke("sayHello", param);

System.out.println(result[0].toString());

}


}

------------------------------------------------------------------------------------------------



cxf-servlet.xml

-----------------

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"

xmlns:soap="http://cxf.apache.org/bindings/soap"

xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

<jaxws:server id="hwService" serviceClass="IHelloWorld"

address="/hwUrl">

<jaxws:serviceBean>

<bean class="HelloWorldImpl" />

</jaxws:serviceBean>

</jaxws:server>

</beans>

------------------------------------------------------------------------------------------------


web.xml

---------

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<display-name></display-name>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<servlet>

<servlet-name>cxfS</servlet-name>

<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>cxfS</servlet-name>

<url-pattern>/ws/*</url-pattern>

</servlet-mapping>

</web-app>

------------------------------------------------------------------------------------------------



部署項目,然後運行Test.java

在瀏覽器裡面輸入http://localhost:8080/cxf/ws/hwUrl?wsdl 可查看webservice服務介面信息

❸ java調用webservice介面具體怎麼調用

Java調用WebService可以直接使用Apache提供的axis.jar自己編寫代碼,或者利用Eclipse自動生成WebService Client代碼,利用其中的Proxy類進行調用。理論上是一樣的,只不過用Eclipse自動生成代碼省事些。
1、編寫代碼方式:
package com.yun.test;
import java.rmi.RemoteException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.message.PrefixedQName;
import org.apache.axis.message.SOAPHeaderElement;
import com.cezanne.golden.user.Exception;
import com.cezanne.golden.user.UserManagerServiceProxy;
import javax.xml.namespace.QName;
import java.net.MalformedURLException;
import javax.xml.rpc.ServiceException;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPException;

public class testWebService {
public static String getResult() throws ServiceException, MalformedURLException, RemoteException, SOAPException
{
//標識Web Service的具體路徑
String endpoint = "WebService服務地址";
// 創建 Service實例
Service service = new Service();
// 通過Service實例創建Call的實例
Call call = (Call) service.createCall();
//將Web Service的服務路徑加入到call實例之中.
call.setTargetEndpointAddress( new java.net.URL(endpoint) );//為Call設置服務的位置
// 由於需要認證,故需要設置調用的SOAP頭信息。
Name headerName = new PrefixedQName( new QName("發布的wsdl里的targetNamespace里的url", "string_itemName") );
org.apache.axis.message.SOAPHeaderElement header = new SOAPHeaderElement(headerName);
header.addTextNode( "blablabla" );
call.addHeader(header);

// SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement("發布的wsdl里的targetNamespace里的url", "SoapHeader");
// soapHeaderElement.setNamespaceURI("發布的wsdl里的targetNamespace里的url");
// try
// {
// soapHeaderElement.addChildElement("string_itemName").setValue("blablabla");
// }
// catch (SOAPException e)
// {
// e.printStackTrace();
// }
// call.addHeader(soapHeaderElement);
//調用Web Service的方法
org.apache.axis.description.OperationDesc oper;
org.apache.axis.description.ParameterDesc param;
oper = new org.apache.axis.description.OperationDesc();
oper.setName("opName");
param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg0"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, false, false);
param.setOmittable(true);
oper.addParameter(param);
param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg1"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, false, false);
param.setOmittable(true);
oper.addParameter(param);
param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg2"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, false, false);
param.setOmittable(true);
oper.addParameter(param);

❹ 如何使用java做webservice

基於AXIS的web service: 1 比如要建一個Server.java類的web service public class Server { public String printInfo(String name){ return "Hello,"+name; } } 2 把Server.java改為Server.Jws放到 …\Tomcat 5.5\webapps\axis中,重啟伺服器 3 訪問 4 在cmd中輸入 cd D:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\axis\WEB-INF 輸入命令:Java -Djava.ext.dirs=lib org.apache.axis.wsdl.WSDL2Java 5 找到…\Tomcat 5.5\webapps\axis\WEB-INF下生成的localhost文件夾復制到工程中 6 建一個Client端的類進行測試: public class Client { public static void main(String arg[]) throws ServiceException, RemoteException{ ServerService ss=new ServerServiceLocator(); Server s=ss.getServer(); System.out.println("............"+s.printInfo("shiyou")); } } 藍屏

❺ Java調WebService完整的實例

首先, 你要先把你的WS服務啟動起來,就是http://localhost:8080/Example/services/HelloWorldService?wsdl
然後在你的另一個項目中建一個Webservice Client 客戶端,用來訪問你的WS服務。
建立Webservice Client 方法如下,在Eclipses中建立一個java工程,然後在src上右鍵--NEW---Other---Web Service Client --Xfire--在WsdL url 中寫上http://localhost:8080/Example/services/HelloWorldService?wsdl-----下一步結束。
在src里的會出現一些java文件,你找一個以Client結束的java文件,在裡面的main方法中會有個service對象,現在你就可以直接用這個對象了,service.peerstatus(參數)這樣寫就行了。
純手打啊,希望能對你有幫助。

熱點內容
手機淘寶緩存視頻 發布:2024-05-21 05:21:09 瀏覽:347
4款配置怎麼選 發布:2024-05-21 05:20:03 瀏覽:585
python服務重啟 發布:2024-05-21 05:07:51 瀏覽:667
內部存儲空間怎麼清除 發布:2024-05-21 04:04:55 瀏覽:498
bilibili不能緩存 發布:2024-05-21 03:31:14 瀏覽:617
解壓剃發 發布:2024-05-21 03:16:27 瀏覽:641
伺服器怎麼連接到電腦顯示屏上 發布:2024-05-21 02:38:21 瀏覽:286
織夢安裝資料庫連接失敗 發布:2024-05-21 02:37:45 瀏覽:259
python編程入門經典pdf 發布:2024-05-21 02:31:45 瀏覽:7
arm編譯添加驅動 發布:2024-05-21 02:02:28 瀏覽:476