webservice的應(yīng)用已經(jīng)越來越廣泛了,下面介紹幾種在Java體系中開發(fā)webservice的方式,相當(dāng)于做個記錄。
1.Axis2
Axis是apache下一個開源的webservice開發(fā)組件,出現(xiàn)的算是比較早了,也比較成熟。這里主要介紹Axis+eclipse開發(fā)webservice,當(dāng)然不用eclipse也可以開發(fā)和發(fā)布webservice,只是用eclipse會比較方便。
(1)下載eclipse的Java EE版本
(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)在%eclipse_home%的目錄下新建links目錄,并在links目錄下新建axis2.link文件,內(nèi)容為:path=D:\programSoftware\eclipse-SVN\Axis2;
4)重啟eclipse,點(diǎn)擊·file-new-other,如果看到Axis2 Wizards,則表明插件安裝成功。
(5)安裝axis2
下載Axis2的WAR Distribution并解壓,把a(bǔ)xis2.war包放置到%TOMCAT_HOME%/webapps下,啟動tomcat,訪問http://localhost:port/axis2,Axis2安裝成功。
(6)使用eclipse新建web工程,創(chuàng)建一個普通java類,至少包含一個方法。
(7)發(fā)布webservice
1)點(diǎn)擊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 theservice xml automatically;
5)Service Name-填寫你的service名稱,Class Name-填寫類名稱,要包括包名,然后點(diǎn)擊load,然后點(diǎn)擊Finish,這時webservice就發(fā)布成功了;
6)然后到%TOMCAT_HOME%/webapps/axis2/WEB-INF/services 看看是否多了一個.aar的文件;
7)訪問http://localhost:8085/axis2/services/類名?wsdl 就可看到生成的wsdl文件了。
注意:以上的方式是發(fā)布到axis2.war包中,你也可以把生成.aar文件copy到你的實(shí)際應(yīng)用中,同時,你也可以使用eclipse的create webservice功能發(fā)布你的webservice,選擇axis2生成你的webservice,這樣webservice就會部署到你的應(yīng)用中了。
2.Apche CXF
CXF開發(fā)webservice也是比較方便和簡單的,它和spring的集成可以說是非常地好。舉一個CXF開發(fā)webservice的例子吧。
1)在eclipse中新建一個web工程,導(dǎo)入依賴包。
2)編寫一個接口,如:
1
|
public String test( @WebParam (name= "value" , targetNamespace = "http://service.cxf.zcl.com/" , mode = WebParam.Mode.IN)String value); |
注意:CXF開發(fā)的webservice,接口中的方法的參數(shù)一定要以這種方式,否則客戶端調(diào)用的時候CXF服務(wù)端會接收不到參數(shù)的值,name:參數(shù)名稱,可不寫(建議寫上),targetNamespace:命名空間,一定要填寫上,默認(rèn)是包名反過來的順序,mode:參數(shù)類型,IN表示輸入。
3)編寫一個實(shí)現(xiàn)類,實(shí)現(xiàn)接口的方法;
4)和spring的集成,編寫一個bean文件,如:cxf-beans.xml,內(nèi)容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<? 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> < import resource = "classpath:META-INF/cxf/cxf.xml" /> < import resource = "classpath:META-INF/cxf/cxf-extension-soap.xml" /> < import resource = "classpath:META-INF/cxf/cxf-servlet.xml" /> < jaxws:endpoint id = "vote" implementor = "com.zcl.cxf.service.VoteImpl" address = "/Vote" /> </ beans > |
這個文件比較容易理解,就不解釋了。
5)配置CXFServlet
在web.xml文件中配置CXFServlet,加載cxf-beans.xml文件,內(nèi)容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
< web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id = "WebApp_ID" version = "2.5" > < context-param > < param-name >contextConfigLocation</ param-name > < param-value >WEB-INF/cxf-beans.xml</ param-value > </ context-param > < listener > < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class > </ listener > < servlet > < servlet-name >cxf</ servlet-name > < servlet-class >org.apache.cxf.transport.servlet.CXFServlet</ servlet-class > < load-on-startup >1</ load-on-startup > </ servlet > < servlet-mapping > < servlet-name >cxf</ servlet-name > < url-pattern >/services/*</ url-pattern > </ servlet-mapping > </ web-app > |
把工程部署到中間件,如tomcat,就可以訪問該webservice了。
3.JDK開發(fā)webservice
1)編寫一個Java類,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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)運(yùn)行該java類,在瀏覽器上就可以訪問該webservice了。
注意:開發(fā)web工程的時候,這種方法不太友好。我們可以編寫一個servlet類,在servlet類的初始化方法中發(fā)布webservice,這樣我們的中間件服務(wù)器啟動的時候就會幫我們自動webservice了。
3) xfire
開發(fā)WebService的框架不少,每個框架都有各自的有點(diǎn),最近我用xfire練習(xí)開發(fā)WebService,下面是開發(fā)WebService的小例子,希望對入門的人有些小幫助
1.新建一個java web project命名為TestWebService,將xfire相關(guān)的jar包添加到lib目錄中,寫接口類和實(shí)現(xiàn)類
1
2
3
4
5
|
package com.lamp.service; public interface MessageService { public String getName(String name); } |
1
2
3
4
5
|
package com.lamp.service; public interface MessageService { public String getName(String name); } |
實(shí)現(xiàn)類
1
2
3
4
5
6
7
8
9
10
11
|
package com.lamp.service.impl; import com.lamp.service.MessageService; public class MessageServiceImpl implements MessageService { public String getName(String name) { return "hellow " + name + ", welcome to WebService world" ; } } |
1
2
3
4
5
6
7
8
9
10
11
|
package com.lamp.service.impl; import com.lamp.service.MessageService; public class MessageServiceImpl implements MessageService { public String getName(String name) { return "hellow " + name + ", welcome to WebService world" ; } } |
在src目錄下新建文件夾META-INF,然后再在其下新建文件夾xfire,在xfire目錄下新建配置文件services.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://xfire.codehaus.org/config/1.0" > < service > < name >MessageService</ name > < serviceClass >com.lamp.service.MessageService</ serviceClass > < implementationClass >com.lamp.service.impl.MessageServiceImpl</ implementationClass > </ service > </ beans > <? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://xfire.codehaus.org/config/1.0" > < service > < name >MessageService</ name > < serviceClass >com.lamp.service.MessageService</ serviceClass > < implementationClass >com.lamp.service.impl.MessageServiceImpl</ implementationClass > </ service > </ beans > |
最后在web.xml中配置xfire的servlet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
< servlet > < servlet-name >XFireServlet</ servlet-name > < servlet-class > org.codehaus.xfire.transport.http.XFireConfigurableServlet </ servlet-class > </ servlet > < servlet-mapping > < servlet-name >XFireServlet</ servlet-name > < url-pattern >/servlet/XFireServlet/*</ url-pattern > </ servlet-mapping > < servlet-mapping > < servlet-name >XFireServlet</ servlet-name > < url-pattern >/services/*</ url-pattern > </ servlet-mapping > < servlet > < servlet-name >XFireServlet</ servlet-name > < servlet-class > org.codehaus.xfire.transport.http.XFireConfigurableServlet </ servlet-class > </ servlet > < servlet-mapping > < servlet-name >XFireServlet</ servlet-name > < url-pattern >/servlet/XFireServlet/*</ url-pattern > </ servlet-mapping > < servlet-mapping > < servlet-name >XFireServlet</ servlet-name > < url-pattern >/services/*</ url-pattern > </ servlet-mapping > |
項(xiàng)目部署后在瀏覽器中通過http://localhost:8080/TestWebService/services訪問看到遠(yuǎn)程訪問的接口,并得到wsdl為http://localhost:8080/TestWebService/services/MessageService?wsdl
這樣服務(wù)器端開發(fā)完畢,現(xiàn)在開始客戶端的開發(fā)
新建一個java project也將xfire相關(guān)的jar引入,我用ant在客戶端生成代理對象,在項(xiàng)目路徑下新建build.xml,代碼為
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<? xml version = "1.0" encoding = "UTF-8" ?> < project name = "WebService" basedir = "." default = "gen-webservice" > < property file = "build.properties" > </ property > < path id = "project-classpath" > < fileset dir = "${lib.dir}" > < include name = "**/*.jar" /> </ fileset > </ path > < target name = "gen-webservice" > < taskdef name = "wsgen" classname = "org.codehaus.xfire.gen.WsGenTask" classpathref = "project-classpath" /> < wsgen outputDirectory = "${src.dir}" wsdl = "${wsdl.dir}" package = "com.lamp.ws.client" overwrite = "true" /> </ target > </ project > |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<? xml version = "1.0" encoding = "UTF-8" ?> < project name = "WebService" basedir = "." default = "gen-webservice" > < property file = "build.properties" > </ property > < path id = "project-classpath" > < fileset dir = "${lib.dir}" > < include name = "**/*.jar" /> </ fileset > </ path > < target name = "gen-webservice" > < taskdef name = "wsgen" classname = "org.codehaus.xfire.gen.WsGenTask" classpathref = "project-classpath" /> < wsgen outputDirectory = "${src.dir}" wsdl = "${wsdl.dir}" package = "com.lamp.ws.client" overwrite = "true" /> </ target > </ project > |
其引入的build.properties文件也在項(xiàng)目路徑下
1
2
3
|
src.dir=${basedir}/src lib.dir=F:/WebService/xfire- 1.2 . 6 /lib wsdl.dir=http: //localhost:8080/TestWebService/services/MessageService?wsdl |
其中l(wèi)ib.jar為我存放xfire的路徑,運(yùn)行ant得到代理對象
編寫一個測試類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.lamp.test; import com.lamp.ws.client.MessageServiceClient; import com.lamp.ws.client.MessageServicePortType; public class TestGetName { public static void main(String[] args) { MessageServiceClient msg = new MessageServiceClient(); MessageServicePortType portType = msg.getMessageServiceHttpPort(); String result = portType.getName( "張三" ); System.out.println(result); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.lamp.test; import com.lamp.ws.client.MessageServiceClient; import com.lamp.ws.client.MessageServicePortType; public class TestGetName { public static void main(String[] args) { MessageServiceClient msg = new MessageServiceClient(); MessageServicePortType portType = msg.getMessageServiceHttpPort(); String result = portType.getName( "張三" ); System.out.println(result); } } |
運(yùn)行在控制臺看到了hellow 張三, welcome to WebService world至此一個簡單的WebService開發(fā)完畢
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/haoxingfeng/article/details/9167757