文章主要介绍:
1:用xfire+spring+maven构建webservice服务器。
2:用xfire的eclipse插件生成客户端访问方式。
3:以知接口和bean生成客户端。
4:客户端动态访问。包括返回java自定义对象。
1:用xfire+spring+maven构建webservice服务器。
(一)肯定首先配置spring上下文监听器和xfire的servert配置,将下列代码加入web.xml中:
<?xml version="1.0" encoding="UTF-8"?> <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"> <display-name>webservice</display-name> <!-- springmvc 上下文监听器 ContextLoaderListener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/*.xml</param-value> </context-param> <!-- springmvc 上下文监听器 ContextLoaderListener--> <servlet> <servlet-name>xfire</servlet-name> <servlet-class> org.codehaus.xfire.spring.XFireSpringServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>xfire</servlet-name> <url-pattern>/service/*</url-pattern> </servlet-mapping> </web-app>
(二)配置 spring的配置文件,最基本的包括导入xfire.xml和baseWebService,剩余2个bean是自定义的webservice接口和实现类。代码如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" /> <bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false" abstract="true"> <property name="serviceFactory" ref="xfire.serviceFactory" /> <property name="xfire" ref="xfire" /> </bean> <bean id="bookWS" class="com.xiaoji.webservice.xfire.service.impl.BookServiceImpl"> </bean> <bean id="bookService" parent="baseWebService"> <property name="serviceBean" ref="bookWS" /> <property name="serviceClass" value="com.xiaoji.webservice.xfire.service.BookService" /> </bean> </beans>
(三)剩下的就是自定义接口和实现类,对象bean.代码如下
book.java
package com.xiaoji.webservice.xfire.entity; public class Book { private int bookId; private String name; public Book(){ } public Book(int bookId,String name){ this.bookId = bookId; this.name = name; } public int getBookId() { return bookId; } public void setBookId(int bookId) { this.bookId = bookId; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
BookService和BookServiceimpl,定义了2个方法,一个传入字符串并返回字符串,另外一个传入个数字返回个对象:
package com.xiaoji.webservice.xfire.service; import com.xiaoji.webservice.xfire.entity.Book; public interface BookService { public Book getBookById(int id); public String sayHello(String str); }
package com.xiaoji.webservice.xfire.service.impl; import java.util.ArrayList; import java.util.List; import com.xiaoji.webservice.xfire.entity.Book; import com.xiaoji.webservice.xfire.service.BookService; public class BookServiceImpl implements BookService { public Book getBookById(int id) { Book book = new Book(1,"china"); return book; } public String sayHello(String str) { // TODO Auto-generated method stub return "this is " + str + "."; } }
最后还是要贴上pom.xml源码
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xiaoji.webservice</groupId> <artifactId>webservice-xfire</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>webservice-xfire Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.codehaus.xfire</groupId> <artifactId>xfire-core</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>org.codehaus.xfire</groupId> <artifactId>xfire-spring</artifactId> <version>1.2.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.2.8.RELEASE</version> </dependency> </dependencies> <build> <finalName>webservice-xfire</finalName> </build> </project>
服务器注解:服务器配置基本这样,有一点不理解的地方就是不能返回List,map等集合对象。如果有人实现可以互相交流哈。
启动jetty,访问127.0.0.1:8080/webservice-xfire/service/BookService?wsdl。
2用xfire的eclipse插件生成客户端访问方式。
如果你eclipse上没有xfire插件,请参考我的博客:http://jishiweili.iteye.com/admin/blogs/2086145,进行xfire插件安装,
新建个maven-archetype-webapp,右键新建的项目,选择new-》other-》Code generation from WSDL document,wsdl url or path为:127.0.0.1:8080/webservice-xfire/service/BookService?wsdl;output directory选择新建项目下的src/java/main;最后输入package(随便输);确定即可。
你会发现包里面多了个xfire组包 可以删掉,新建个客户端类,代码如下:
package com.xiaoji.webservice.xfire.client; import java.net.MalformedURLException; public class WebserviceXfireClient { public static void main(String[] args) throws Exception { //xfire客户端访问webservice第3种方式 //xfire自动生成客户端 BookServiceClient client = new BookServiceClient(); BookServicePortType bs = client.getBookServiceHttpPort(); System.out.println(bs.sayHello("小吉")); System.out.println(bs.getBookById(0).getName().getValue()); } }
最后贴上xfire的pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xiaoji.webservice.xfire</groupId> <artifactId>eclipseplugin</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>eclipseplugin Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.codehaus.xfire</groupId> <artifactId>xfire-all</artifactId> <version>1.2.6</version> </dependency> </dependencies> <build> <finalName>eclipseplugin</finalName> </build> </project>
3:第二种访问方式是知道接口的自定义对象bean时用,这里要注意,必须类名和包名一直,要不访问不到的。
这里只贴客户端代码。pom.xml,类和接口如上,
// 第一种访问方式需要知道接口和bean public static void testClient1() throws MalformedURLException { String serviceUrl = "http://172.16.141.55:8080/webservice-xfire/service/BookService"; XFire xfire = XFireFactory.newInstance().getXFire(); XFireProxyFactory factory = new XFireProxyFactory(xfire); Service service = new ObjectServiceFactory().create(BookService.class); BookService bs = (BookService) factory.create(service, serviceUrl); System.out.println(bs.sayHello("小吉111111")); System.out.println(bs.getBookById(0).getName()); }
4:第4中客户端动态访问,这里我刚刚开始的时候始终不能返回自定义对象,返回的类型为DocumentImpl,我就想办法解析这个类型查找类型相关资料,终于解析成功,这里共享给大家:
// 动态客户端第2种访问方式 public static void testClient2() throws MalformedURLException, Exception { Client client = new Client( new URL( "http://172.16.141.55:8080/webservice-xfire/service/BookService?wsdl")); Object[] results = client.invoke("sayHello", new Object[] { "吉凌夷" }); Object[] results2 = client.invoke("getBookById", new Object[] { 1 }); System.out.println(results[0]); //返回对象解析 Document xmlTree = (Document)results2[0]; Element root = xmlTree.getDocumentElement(); parseElement(root); System.out.println(map.get("bookId")+"->"+map.get("name")); } /** * 解析返回树 * @param root */ private static void parseElement(Element root) { String key = root.getNodeName(); NodeList list = root.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); if (node instanceof Element) { Element e = (Element) node; parseElement(e); } else if (node instanceof Text) { Text t = (Text) node; map.put(key, t.getNodeValue()); } } }
注:pom.xml和第2中是一样的,这里要说一下xfire,官网很久没更新,官网上说的是和cxf合并了,具体咋样大家有待考证。
剩下的就是axis2,也是资料最多的。如果不是很忙的话我会很快更新出来。
下面我会上传源码:其中一个是服务器,2个是客户端,第2中访问和第3中访问我放在一起,请自行分别。