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

webservicejava實例

發布時間: 2023-08-07 16:26:53

A. java怎樣提供webservice介面

Java通過WSDL文件來調用webservice:

注意,以下的代碼並沒有經悄扮過真正的測試,只是說明肢運禪這些情況,不同版本的Axis相差很大,大家最好以apache網站上的例子為准,這里僅僅用於說明其基本用法。
1,直接AXIS調用遠程的web service
這種方法比較適合那些高手,他們能直接看懂XML格式的WSDL文件,我自己是看不懂的,尤其我不是專門搞這行的,即使一段時間看懂,後來也就忘記了。歷塵直接調用模式如下:
import java.util.Date;
import java.text.DateFormat;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import java.lang.Integer;
import javax.xml.rpc.ParameterMode;

public class caClient {

public static void main(String[] args) {

try {
String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";
//直接引用遠程的wsdl文件
//以下都是套路
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName("addUser");//WSDL裡面描述的介面名稱
call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,
javax.xml.rpc.ParameterMode.IN);//介面的參數
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//設置返回類型

String temp = "測試人員";
String result = (String)call.invoke(new Object[]{temp});
//給方法傳遞參數,並且調用方法
System.out.println("result is "+result);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}
2,直接SOAP調用遠程的webservice
這種模式我從來沒有見過,也沒有試過,但是網路上有人貼出來,我也轉過來
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;

import java.io.*;
import java.net.*;
import java.util.Vector;

public class caService{
public static String getService(String user) {
URL url = null;
try {
url=new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized");
} catch (MalformedURLException mue) {
return mue.getMessage();
}
// This is the main SOAP object
Call soapCall = new Call();
// Use SOAP encoding
soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
// This is the remote object we're asking for the price
soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");
// This is the name of the method on the above object
soapCall.setMethodName("getUser");
// We need to send the ISBN number as an input parameter to the method
Vector soapParams = new Vector();

// name, type, value, encoding style
Parameter isbnParam = new Parameter("userName", String.class, user, null);
soapParams.addElement(isbnParam);
soapCall.setParams(soapParams);
try {
// Invoke the remote method on the object
Response soapResponse = soapCall.invoke(url,"");
// Check to see if there is an error, return "N/A"
if (soapResponse.generatedFault()) {
Fault fault = soapResponse.getFault();
String f = fault.getFaultString();
return f;
} else {
// read result
Parameter soapResult = soapResponse.getReturnValue ();
// get a string from the result
return soapResult.getValue().toString();
}
} catch (SOAPException se) {
return se.getMessage();
}
}
}
3,使用wsdl2java把WSDL文件轉成本地類,然後像本地類一樣使用,即可。
這是像我這種懶人最喜歡的方式,仍然以前面的global weather report為例。
首先 java org.apache.axis.wsdl.WSDL2Java http://www.webservicex.net/globalweather.asmx.WSDL
原本的網址是http://www.webservicex.net/globalweather.asmx?WSDL,中間個各問號,但是Linux下面它不能解析,所以去掉問號,改為點號。
那麼就會出現4個文件:
GlobalWeather.java GlobalWeatherLocator.java GlobalWeatherSoap.java GlobalWeatherSoapStub.java
其中GlobalWeatherSoap.java是我們最為關心的介面文件,如果你對RMI等SOAP實現的具體細節不感興趣,那麼你只需要看介面文件即可,在使用的時候,引入這個介面即可,就好像使用本地類一樣。

B. 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服務介面信息

C. 用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,第二處表示公共的方法名稱。

D. java如何寫webservice服務端

Java 中的 Web Service 分為基於 SOAP 的和基於 REST 的兩種,下面簡單說一個基於 SOAP 的例子。要使用 JDK6u4 之後的版本才能編譯通過。

先編寫一個 Web Service 的介面:

@WebService
@SOAPBinding(style=Style.RPC)
publicinterfaceTimeServer{
@();
@WebMethodlonggetTimeAsElapsed();
}

再編寫 Web Service 實現:

importjava.util.Date;
importjavax.jws.WebService;
@WebService(endpointInterface="test.TimeServer")
{
publicStringgetTimeAsString(){returnnewDate().toString();}
publiclonggetTimeAsElapsed(){returnnewDate().getTime();}
}

最後啟動 Web Service:

{
publicstaticvoidmain(String[]args){
Endpoint.publish("http://127.0.0.1:9876/ts",newTimeServerImpl());
}
}


如果正常啟動,可以用瀏覽器訪問 http://127.0.0.1:9876/ts?wsdl 看到這個 Web Service 的 wsdl 文檔。

熱點內容
phpsocket教程 發布:2025-02-06 17:42:13 瀏覽:421
mysql解壓縮版安裝 發布:2025-02-06 17:26:33 瀏覽:179
phpgd圖片 發布:2025-02-06 17:24:34 瀏覽:206
php代碼整理 發布:2025-02-06 17:24:31 瀏覽:477
java可執行文件 發布:2025-02-06 17:18:59 瀏覽:249
衛士相當於現在什麼配置 發布:2025-02-06 17:05:04 瀏覽:409
項目編譯慢 發布:2025-02-06 16:53:48 瀏覽:382
python處理excel文件 發布:2025-02-06 16:36:09 瀏覽:443
演算法相對定位 發布:2025-02-06 16:32:42 瀏覽:728
java程序的編譯和執行 發布:2025-02-06 16:21:45 瀏覽:420