本文目錄一覽:
php做客戶端,java做服務端,用webservice怎麼交互
.java編寫webservice服務端,php作為客戶端調用.
1.首先我們寫一個簡單的java類並發布webservice.
package com.php;
import java.util.Map;
/**
* @author yangjuqi
* @createdate 2009-5-18 下午04:43:09
*
*/
public class WebServiceImpl {
public String sendTransact(Map map) throws Exception {
System.out.println(“::: Call testModel1 :::”);
if(map!=null){
String bugmanifestid = StringUtil.getValue(map.get(“bugmanifestid”));
String editedby = StringUtil.getValue(map.get(“editedby”));
String dditeddate = StringUtil.getValue(map.get(“dditeddate”));
String fullinfo = StringUtil.getValue(map.get(“fullinfo”));
String action = StringUtil.getValue(map.get(“action”));
System.out.println(“bugmanifestid -$amp;quot;$ +bugmanifestid);
System.out.println(“editedby -$amp;quot;$ +editedby);
System.out.println(“dditeddate -$amp;quot;$ +dditeddate);
System.out.println(“fullinfo -$amp;quot;$ +fullinfo);
System.out.println(“action -$amp;quot;$ +action);
}
return “success”;
}
}
2.配置server-config.wsdd
deployment xmlns=””
xmlns:java=””
handler name=”URLMapper”
type=”java:org.apache.axis.handlers.http.URLMapper” /
handler name=”auth”
type=”java:com.php.AuthenticationHandler” /
handler name=”URLLogging”
type=”java:com.php.LogHandler”
parameter name=”filename” value=”c:\\MyService.log” /
/handler
service name=”IWebService” provider=”java:RPC”
parameter name=”className”
value=”com.php.WebServiceImpl” /
parameter name=”allowedMethods” value=”*” /
namespace;/namespace
/service
transport name=”http”
requestFlow
handler type=”URLMapper” /
handler type=”URLLogging” /
/requestFlow
/transport
/deployment
3.發布到jboss後,訪問 wsdl能看到xml文件就說明webservice發布好了。
4.寫testphpweb.php文件
php
/*
* @author juqi yang $amp;amp;$gt;
* @create date 2009-05-18
*/
header(“Content-Type: text/html; charset=GB2312”);
echo ” ::: PHP CALL JAVA-WEBSERVICE ::: br$amp;quot;$;
require_once(“nusoap/lib/nusoap.php”);
// 要訪問的webservice路徑
$NusoapWSDL=” wsdl”;
// 生成客戶端對象
$client = new soapclient($NusoapWSDL, true);
// 設置參數(注意:PHP只能以’數組集’方式傳遞參數,如果服務端是java,用Map接收)
$param = array( ‘bugmanifestid’ = ‘E090500001’,
‘editedby’ = ‘張三’,
‘dditeddate’ = ‘2009-05-19’,
‘fullinfo’ = ‘已聯繫劉德華,籌備今晚吃飯的事,等待回復’,
‘action’ = ‘0’);
echo “begin remote 。。。br$amp;quot;$;
// 調用遠程方法
$result = $client-call(‘sendTransact’, array($param));
echo “end remote 。。。br$amp;quot;$;
// 顯示執行結果
if (!$err=$client-getError()){
echo ‘結果 : ‘.$result;
}else{
echo ‘錯誤 : ‘.$err;
}
5.啟動apache,訪問
php頁面顯示:
::: PHP CALL JAVA-WEBSERVICE :::
begin remote 。。。
end remote 。。。
結果 : success
jboss後台監視結果:
17:12:20,781 INFO [STDOUT] ::: Call testModel1 :::
17:12:20,781 INFO [STDOUT] bugmanifestid -E090500001
17:12:20,781 INFO [STDOUT] editedby -張三
17:12:20,781 INFO [STDOUT] dditeddate -2009-05-19
17:12:20,781 INFO [STDOUT] fullinfo -已聯繫劉德華,籌備今晚吃飯的事,等待回復
17:12:20,796 INFO [STDOUT] action -0
到此,php作為客戶端調用java寫的webservice服務端完成.
二,php編寫webservice服務端,java作為客戶端調用.
1.編寫php webservice
php
/*
* @author juqi yang $amp;amp;$gt;
* @create date 2009-05-18
*/
header(“Content-Type: text/html; charset=GB2312”);
require_once(“nusoap/lib/nusoap.php”);
function sendManifest($param)
{
//把接收到的數據顯示出來
return “hello “.$param[“projectid”].”=$amp;quot;$.$param[“projectname”].”=$amp;quot;$.$param[“moduleid”];
}
$server = new nusoap_server();
//配置WSDL namespace
$server-configureWSDL(‘myservice’, //服務名稱
”, //tns指定的namespace,一般填寫自己的URI
true, //endpoint url or false
‘rpc’, //服務樣式
”, //傳輸協議,一直是這個。
” //wsdl ‘types’元素targetNamespace
);
// 註冊web服務
$server-register(‘sendManifest’, // 服務
array(
‘projectid’ = ‘xsd:string’,
‘projectname’ = ‘xsd:string’,
‘moduleid’ = ‘xsd:string’,
‘modulepath’ = ‘xsd:string’,
‘bugtitle’ = ‘xsd:string’,
‘bugtype’ = ‘xsd:string’,
‘openedby’ = ‘xsd:string’,
‘openeddate’ = ‘xsd:string’,
‘assignedto’ = ‘xsd:string’,
‘assigneddate’ = ‘xsd:string’,
‘fixedtime’ = ‘xsd:string’,
‘fullinfo’ = ‘xsd:string’,
‘bugmanifestid’ = ‘xsd:string’), // 輸入參數;數組,指定類型
array(‘resultCode’ = ‘xsd:string’), // 輸出;數組,指定類型
”, // namespace of method
”, // soapaction
‘rpc’, // style
‘encoded’, // use
‘serviceConsumeNotify’ // documentation
);
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) $HTTP_RAW_POST_DATA : ”;
$server-service($HTTP_RAW_POST_DATA);
2.啟動apache後,訪問 ,如果頁面如下圖所示,表示webservice發布好了。
3.編寫java客戶端CallPhpServer .java 並調用php webservice
package com.php;
import java.util.HashMap;
import java.util.Map;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
/**
* @author yangjuqi
* @createdate 2009-5-18 下午05:31:06
*
*/
public class CallPhpServer {
/**
* 測試方法
* @return
* @throws Exception
*/
public static String callManifest() throws Exception {
System.out.println(“0”);
Service service = new Service();
Call call = (Call) service.createCall();
System.out.println(“1”);
call.setTargetEndpointAddress(new java.net.URL(“”));
call.setOperationName(“sendManifest”);
System.out.println(“2”);
Map map=new HashMap();
map.put(“projectid”, “109”);
map.put(“projectname”, new String(“新MM國際物流平台”.getBytes(),”iso-8859-1″));
map.put(“moduleid”, “11”);
map.put(“modulepath”, new String(“財務管理”.getBytes(),”iso-8859-1″));
map.put(“bugtitle”, new String(“關於總賬報表數據的問題”.getBytes(),”iso-8859-1″));
map.put(“bugtype”, “TrackThings”);
map.put(“openedby”, “zhangsan”);
map.put(“openeddate”, “2009-05-31”);
map.put(“assignedto”, “liumang”);
map.put(“assigneddate”, “2009-05-31”);
map.put(“fixedtime”, “2009-06-03”);
map.put(“fullinfo”, new String(“現在總賬報表頁面下的合計數據不對,煩請抓緊事件核實確認更正,謝謝!”.getBytes(),”iso-8859-1″));
map.put(“bugmanifestid”, “E090500001”);
call.addParameter(“param”, org.apache.axis.Constants.SOAP_ARRAY,javax.xml.rpc.ParameterMode.IN);
call.setReturnType(org.apache.axis.Constants.XSD_STRING);
System.out.println(“3”);
Object obj=call.invoke(new Object[]{map});
return obj.toString();
}
public static void main(String[] args) throws Exception {
System.out.println(“::: call php webservice :::”);
String str = callManifest();
String result=new String(str.getBytes(“iso-8859-1″),”GBK”);
System.out.println(result);
}
}
控制台顯示結果:
::: call php webservice :::
log4j:WARN No appenders could be found for logger (org.apache.axis.i18n.ProjectResourceBundle).
log4j:WARN Please initialize the log4j system properly.
1
2
3
hello 109=新MM國際物流平台=11
到此,java作為客戶端調用php的webservice服務端完成.
app服務端可以用java開發么
可以的,java最適合作為app服務端了,直接暴露rest服務就可以了。
REST 系統中所有的動作和要訪問的資源都可以從H TTP和URI 中得到,這使得代理伺服器、緩存伺服器和網關很好地協調工作。而RPC模 型的SOAP 要訪問的資源僅從 URI無法得知,要調用的方法也無法從 HTTP中得知,它們都隱藏在 SOAP 消息中。同樣的,在REST系統中的代理伺服器還可以通過 HTTP 的動作 (GET 、 POST)來進行控制。
java如何寫webservice服務端
Java 中的 Web Service 分為基於 SOAP 的和基於 REST 的兩種,下面簡單說一個基於 SOAP 的例子。要使用 JDK6u4 之後的版本才能編譯通過。
先編寫一個 Web Service 的介面:
@WebService
@SOAPBinding(style = Style.RPC)
public interface TimeServer {
@WebMethod String getTimeAsString();
@WebMethod long getTimeAsElapsed();
}
再編寫 Web Service 實現:
import java.util.Date;
import javax.jws.WebService;
@WebService(endpointInterface = “test.TimeServer”)
public class TimeServerImpl implements TimeServer {
public String getTimeAsString() { return new Date().toString(); }
public long getTimeAsElapsed() { return new Date().getTime(); }
}
最後啟動 Web Service:
public class TimeServerPublisher {
public static void main(String[ ] args) {
Endpoint.publish(“”, new TimeServerImpl());
}
}
如果正常啟動,可以用瀏覽器訪問 看到這個 Web Service 的 wsdl 文檔。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/296086.html