- 相關(guān)推薦
XFire創(chuàng)建WebService實(shí)例
XFire使得在JavaEE應(yīng)用中發(fā)布Web服務(wù)變得輕而易舉。和其他Web服務(wù)引擎相比,XFire的配置非常簡(jiǎn)單,可以非常容易地和Spring集成。那么XFire怎么創(chuàng)建WebService,下面yjbys小編為大家分享XFire創(chuàng)建WebService簡(jiǎn)單實(shí)例:
一. 使用XFire發(fā)布WebService
1. 創(chuàng)建service接口
Java代碼
1. package com.test.service;
2. import com.test.service.bean.User;
3.
4. public interface IHelloService {
5. public String sayHello(String name);
6. public User getUser(User user);
7. }
8. }
2.創(chuàng)建Service接口的實(shí)現(xiàn)類
Java代碼
1. package com.test.service;
2. import com.test.service.bean.User;
3. public class HelloService implements IHelloService{
4. public String sayHello(String name){
5. return "Hello, "+name;
6. }
7. public User getUser(User user) {
8. User userNew = new User();
9. userNew.setId("new:"+user.getId());
10. userNew.setName("new:"+user.getName());
11. return userNew;
12. }
13.}
Java代碼
1. package com.test.service.bean;
2. public class User {
3. private String id;
4. private String name;
5. public String getId() {
6. return id;
7. }
8. public void setId(String id) {
9. this.id = id;
10. }
11. public String getName() {
12. return name;
13. }
14. public void setName(String name) {
15. this.name = name;
16. }
17.}
3.在web.xml文件中進(jìn)行XFire攔截配置,可參照網(wǎng)提供的sample。
Xml代碼
1.
2.<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
5. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
6.
7.
8.
9.
10. org.codehaus.xfire.transport.http.XFireConfigurableServlet
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
4.在class目錄下建立META-INF目錄,如(META-INF>xifre->services.xml),
在services.xml文件中進(jìn)行webservice服務(wù)的發(fā)布,可參照網(wǎng)提供的sample。
Xml代碼
1.
2.
3.
4.
5.
6.
7.
8.
5.將xfire的Jar包和相關(guān)依賴包拷到系統(tǒng)應(yīng)用中,在Tomcat下部署應(yīng)用。
啟動(dòng)tomcat后訪問(wèn)服務(wù)(需在應(yīng)用后加上/services,webservice才會(huì)進(jìn)行攔截):
例:http://localhost:8080/ws2/services。(IE下有時(shí)無(wú)法顯示列表,可用其它瀏覽器顯示或指定接口名稱)
界面如下:
點(diǎn)擊后查看詳細(xì)的wsdl文檔,不同的瀏覽器下訪問(wèn)會(huì)有區(qū)別,展示效果不一致。
二.建立XFire客戶端進(jìn)行調(diào)用
1.本地客戶端調(diào)用,與webservice服務(wù)在同一應(yīng)用。
Java代碼
1. package com.test.client;
2.
3. import java.net.MalformedURLException;
4. import org.codehaus.xfire.XFireFactory;
5. import org.codehaus.xfire.client.XFireProxyFactory;
6. import org.codehaus.xfire.service.Service;
7. import org.codehaus.xfire.service.binding.ObjectServiceFactory;
8. import com.test.service.IHelloService;
9. import com.test.service.bean.User;
10.
11.public class ClientTest {
12. public static void main(String args[]) throws MalformedURLException {
13. Service service = new ObjectServiceFactory().create(IHelloService.class);
14. XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
15. .newInstance().getXFire());
16. String url = "http://localhost:8080/ws2/services/HelloService";
17. IHelloService helloService = (IHelloService) factory.create(service,url);
18. System.out.println(helloService.sayHello("張三"));
19. User user = new User();
20. user.setName("張三");
21. System.out.println(helloService.getUser(user).getName());
22. }
23.}
執(zhí)行結(jié)果如下:
Java代碼
1. Hello, 張三
2. new:張三
2. 利用XFire創(chuàng)建build.xml文件生成客戶端代碼調(diào)用webservice服務(wù)
2.1在src目錄下創(chuàng)建build.properties文件,配置如下:
Java代碼
1. src.dir=${basedir}
2. lib.dir=D:/myspace/ws2/WebRoot/WEB-INF/lib
3. wsdl.dir=http://localhost:8080/ws2/services/HelloService?wsdl
2.2在src目錄下創(chuàng)建build.xml文件,配置如下:
Xml代碼
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.<taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask"
12. classpathref="project-classpath" />
13.<wsgen outputDirectory="${src.dir}" wsdl="${wsdl.dir}"
14. package="stub.test.client" overwrite="true"/>
15.
16.
用ant構(gòu)建生成代碼,生成后的目錄如下:
2.3編寫客戶端代碼進(jìn)行調(diào)用
Java代碼
1. package com.test.client;
2. import stub.test.client.HelloServiceClient;
3. import stub.test.client.HelloServicePortType;
4. import com.test.service.bean.ObjectFactory;
5. import com.test.service.bean.User;
6.
7. public class HelloServiceTest {
8.
9. public static void main(String args[]){
10. HelloServiceClient service = new HelloServiceClient();
11. HelloServicePortType portType = service.getHelloServiceHttpPort();
12. System.out.println(portType.sayHello("張三"));
13.
14. ObjectFactory factory = new ObjectFactory();
15. User user = factory.createUser();
16. user.setName(factory.createUserName("張三"));
17. System.out.println(portType.getUser(user).getName().getValue());
18. }
19.}
執(zhí)行結(jié)果如下:
Java代碼
1. Hello, 張三
2. new:張三
【XFire創(chuàng)建WebService實(shí)例】相關(guān)文章:
包裝條款實(shí)例整理04-16
數(shù)控機(jī)床PMC故障診斷與實(shí)例分析01-20
關(guān)于中小企業(yè)融資難的實(shí)例分析04-08
h3c交換機(jī)配置telnet實(shí)例教程04-01
品牌創(chuàng)建的成功與失敗原因04-12
ASP.NET MVC異常處理模塊簡(jiǎn)單教程-ASP.NET教程實(shí)例推薦08-28
創(chuàng)建森林城市征文800字(精選28篇)08-30