javaaxis2
㈠ 問個比較弱的問題,我剛接觸webservice ,axis和axis2 有什麼區別啊
Axis全稱Apache eXtensible Interaction System,是apache組織下的一個開源項目,用來做webservice開發的,有java版的實現,也有c++版的實現,其前身最早是IBM的SOAP4J,後來捐給Apache後改名Apache SOAP,也就是支持SOAP1.1的那個版本,到現在的AXIS已經是第三代了,AXIS對Apache SOAP的改進有很多,比如用SAX代替以前的DOM,增加對WSDL的支持,支持SOAP1.2等等
使用axis很easy,在apache網站上把axis下載了後(最新版本1.2.1),把%AXIS_HOME%\webapps\axis整個目錄到tomcat的webapps目錄下面,啟動tomcat,瀏覽器中輸入http://localhost:8080/axis就可以了
上面說的只是axis運行起來了,要想布置自己的webservice,axis支持兩種方法:一種是用JWS(Java Web Service),好處是只要有源碼比如說Hello.java,把他改成Hello.jws後放到%TOMCAT_HOME%\webapps\axis目錄下面就搞定,瀏覽器裡面輸入http://localhost:8080/axis/Hello.jws就可以了,不足的地方是能配置的地方有限(比如說Hello.java裡面的所有public非static方法都會暴露),很多東西沒辦法弄;第二種方法就是使用wsdd(web service deploy description),這個就是在TOMCAT_HOME%\webapps\axis\WEB-INF目錄下寫一個deploy.wsdd和undeploy.wsdd文件,然後通過java org.apache.axis.client.AdminClient deploy.wsdd就搞定了,卸載這個webservice就用org.apache.axis.client.AdminClient undeploy.wsdd,客戶端輸入http://localhost:8080/axis/services/Hello就可以了
介紹了部署webservice的兩種方法後,下面說說客戶端的訪問方式,也有兩種,一種是通過axis實現的java裡面的JAX-RPC進行動態調用,另外一種就是通過伺服器端產生的wsdl文件利用WSDL2Java工具生成Client Stub後訪問。通過JAX-RPC的比較簡單,需要提供的有SOAP router URL,service namespace,operation name,operation parameter types,operation return types,看看下面這個例子就明白了:
public SOAPWrapper() {
String location = "http://localhost:8080/axis/services/BookService";
try {
Service service = new Service();
call = (Call)service.createCall();
call.setTargetEndpointAddress(new url(/location));
} catch (Throwable t) {
handleError("SOAPWrapper (constructor) had an Exception.",t);
}
} public void create(Book book) {
try {
call.removeAllParameters();
call.setOperationName("create");
call.addParameter("arg1",qn,ParameterMode.IN);
call.setReturnType(XMLType.AXIS_VOID);
call.invoke(new Object[] {book});
return;
} catch (Throwable t) {
handleError("SOAPWrapper (create) had an Exception.",t);
return;
}
}
程序中的Call和Service是對JAX-RPC中Call和Service的具體實現:org.apache.axis.client.Call implements javax.xml.rpc.Call,org.apache.axis.client.Service implements javax.xml.rpc.Service
第二種客戶端的方法就是利用WSDL2Java產生Client stub後通過stub調用,WSDL2Java的操作對象是service所對應的wsdl文件,這個文件怎麼產生呢?一種方法前面是由axis自動產生,如前面伺服器端的配置所說,無論通過jws或是wsdd的方式,axis都會產生相應的wsdl;還有一種方式就是利用Java2WSDL對java源文件進行操作也可以產生相應的wsdl文件。
㈡ 怎麼用 Axis2 把java文件生成wsdl文件
其實,不用特意生成的,部署後,WEB訪問,就生成了
㈢ java axis2 webservice 服務端區怎麼寫
webservice的應用已經越來越廣泛了,下面介紹幾種在Java體系中開發webservice的方式,相當於做個記錄。
1.Axis2
Axis是apache下一個開源的webservice開發組件,出現的算是比較早了,也比較成熟。這里主要介紹Axis+eclipse開發webservice,當然不用eclipse也可以開發和發布webservice,只是用eclipse會比較方便。
(1)下載eclipse的Java EE版本
http://www.eclipse.org/downloads/
(2)下載axis2
http://axis.apache.org/axis2/java/core/download.cgi
(3)下載eclipse的axis2插件
Axis2_Codegen_Wizard
Axis2_Service_Archiver
http://axis.apache.org/axis2/java/core/tools/index.html
推薦使用1.3的版本
(4)eclipse安裝axis2插件
1)在任意目錄下新建一個Axis2文件夾,在該文件夾下新建eclipse目錄,在eclipse目錄中新建plugins目錄和 features目錄,例如:D:\programSoftware\eclipse-SVN\Axis2\eclipse;
2)把下載的axis2插件解壓,並把解壓的文件放到新建的eclipse的plugins目錄下;
3)在�lipse_home%的目錄下新建links目錄,並在links目錄下新建axis2.link文件,內容為: path=D:\programSoftware\eclipse-SVN\Axis2;
4)重啟eclipse,點擊·file-new-other,如果看到Axis2 Wizards,則表明插件安裝成功。
(5)安裝axis2
下載Axis2的WAR Distribution並解壓,把axis2.war包放置到%TOMCAT_HOME%/webapps下,啟動tomcat,訪問http://localhost:port/axis2,Axis2安裝成功。
(6)使用eclipse新建web工程,創建一個普通java類,至少包含一個方法。
(7)發布webservice
1)點擊eclipse的File-New-other,打開Axis2 Wizards,選擇Axis2 Service Archiver,然後Next;
2)選擇Class File Location,也就是類文件存放路徑,注意:只選到classes目錄,不要包括包文件夾,然後 Next;
3)選擇Skip WSDL,然後Next
4)一路Next到Select the Service XML file to be included in the Service archive,勾選Generate the service xml automatically;
5)Service Name-填寫你的service名稱,Class Name-填寫類名稱,要包括包名,然後點擊load,然後點擊 Finish,這時webservice就發布成功了;
6)然後到%TOMCAT_HOME%/webapps/axis2/WEB-INF/services 看看是否多了一個.aar的文件;
7)訪問http://localhost:8085/axis2/services/類名?wsdl 就可看到生成的wsdl文件了。
注意:以上的方式是發布到axis2.war包中,你也可以把生成.aar文件到你的實際應用中,同時,你也可以使用eclipse的create webservice功能發布你的webservice,選擇axis2生成你的webservice,這樣webservice就會部署到你的應用中了。
2.Apche CXF
CXF開發webservice也是比較方便和簡單的,它和spring的集成可以說是非常地好。舉一個CXF開發webservice的例子吧。
1)在eclipse中新建一個web工程,導入依賴包,如圖:
2)編寫一個介面,如:
public String test(@WebParam(name="value", targetNamespace = "http://service.cxf.zcl.com/", mode = WebParam.Mode.IN)String value);
注意:CXF開發的webservice,介面中的方法的參數一定要以這種方式,否則客戶端調用的時候CXF服務端會接收不到參數的值,name:參數名稱,可不寫(建議寫上),targetNamespace:命名空間,一定要填寫上,默認是包名反過來的順序,mode:參數類型,IN表示輸入。
3)編寫一個實現類,實現介面的方法;
4)和spring的集成,編寫一個bean文件,如:cxf-beans.xml,內容如下:
Cxf-beans.xml代碼
這個文件比較容易理解,就不解釋了。
5)配置CXFServlet
在web.xml文件中配置CXFServlet,載入cxf-beans.xml文件,內容如下:
Web.xml代碼
把工程部署到中間件,如tomcat,就可以訪問該webservice了。
3.JDK開發webservice
1)編寫一個Java類,如下:
Jdkwebservice.java代碼
package demo;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class JdkWebService {
public String doSomething(@WebParam(name="value", targetNamespace = "http://demo/", mode = WebParam.Mode.IN)String value) {
return "Just do it," + value + "!";
}
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/jdkwsdemo/demo.JdkWebService", new JdkWebService());
}
}
2)運行該java類,在瀏覽器上就可以訪問該webservice了。
注意:開發web工程的時候,這種方法不太友好。我們可以編寫一個servlet類,在servlet類的初始化方法中發布webservice,這樣我們的中間件伺服器啟動的時候就會幫我們自動webservice了。
㈣ java webservice axis2 傳遞String數組怎麼寫
你是從伺服器端傳遞到客戶端還是客戶端到伺服器端?
伺服器到客戶端可以直接傳遞數組過去的,客戶端是不能傳遞數組過去的,牽扯到序列化的問題
㈤ java如何通過eclipse安裝axis2來調用webservice
調用這個webservice不需要安裝axis2,但是可以在你的工程里引入Axis2的包來編寫一個RPC客戶端程序。
importjavax.xml.namespace.QName;importorg.apache.axis2.AxisFault;
importorg.apache.axis2.addressing.EndpointReference;
importorg.apache.axis2.client.Options;
importorg.apache.axis2.rpc.client.RPCServiceClient;
按照RPC的方式(可以搜搜,網上很多)寫一個調用函數就ok了,比如:
publicObjectinvokeWebservice()throwsAxisFault{
RPCServiceClientserviceClient=newRPCServiceClient();
Optionsoptions=serviceClient.getOptions();
EndpointReferencetargetEPR=newEndpointReference("http://www.webxml.com.cn/WebServices/WeatherWS.asmx?WSDL");
options.setTo(targetEPR);
options.setTimeOutInMilliSeconds(TIME_OUT);
QNameopAddEntry;
Object[]opAddEntryArgs=null;
Class<?>[]classes=newClass<?>[]{String.class};
opAddEntry=newQName("","");
returnserviceClient.invokeBlocking(opAddEntry,opAddEntryArgs,classes)[0];
}
㈥ java 中怎樣用axis2調用webservice服務
ServiceClientsender=newServiceClient();
Optionsoption=newOptions();
option.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
option.setAction("http://WebXml.com.cn/getWeatherbyCityName");
EndpointReferenceepfs=newEndpointReference(address);
option.setTransportInProtocol(Constants.TRANSPORT_HTTP);
option.setTo(epfs);
sender.setOptions(option);
OMFactoryfac=OMAbstractFactory.getOMFactory();
OMNamespaceomNs=fac.createOMNamespace("http://WebXml.com.cn/","");
OMElementdata=fac.createOMElement("getWeatherbyCityName",omNs);
OMElementinner=fac.createOMElement("theCityName",omNs);
inner.setText("長沙");
data.addChild(inner);
OMElementresult=sender.sendReceive(data);
System.out.println(result);
㈦ 通過axis2生成客戶端java代碼怎麼找伺服器端地址
生成的客戶端代碼中有一個名稱以"ServiceLocator"結尾的類,裡面以"_address"結尾的變數就是服務端地址。
㈧ axis2 java webservice 命名空間問題
一般是通過Service添加Endpoint來指定QName的
Endpoint EChannelServicePortEP = service0 .addEndpoint(new QName("http://practise.my.com/webservice/eChannel", "EServicePort"), new QName("http://practise.my.com/webservice/eChannel", "EServiceSoapBinding"),
㈨ Java使用axis2調用一個.net 的webservice,報錯java.lang.NoSuchMethodError: doWriteMTOM
這個問題很簡單,有幾個地方可能會導致這個問題。
1、命名空間,在設置調用action的時候指定action的命名空間,具體可以看wsdl文件中的聲明。setOperationName(new QName("http://tempuri.org/HelloWorld","HelloWorld"))
2、http協議版本不同造成的,axis2在做http傳輸時採用的chunked模式,而.net的webserver不支持axis中使用的是HTTP/1.0協議,而.NET和axis2使用的是HTTP/1.1協議,後兩者的區別在於NET未使用ns1的命名空間前綴打包SOAP請求,且axis2使用了Content- Encoding: chunked頭。所以必須在axis2中設置一下。
UserServiceStub stub = new UserServiceStub();
stub._getServiceClient().getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.CHUNKED,Boolean.FALSE);
具體的還要你試一下,如果方便的話把wsdl文件和java代碼發給我,我看看是哪的問題。
㈩ java axis2 調用webservice 怎麼捕獲超時異常(即超時了讓它停下來,不要報錯)
在axis2.xml可以設置connection超時時間,超時後會拋出AxisFault,直接捕獲這個異常就好了。