XFire发布WebService
编写XFire必须的Web服务描述文件services.xml
该文件必须放到META-INF/xfire/ 目录下
最后,在web.xml中加入
启动服务器,浏览器中输入 http://localhost:8080/services/BookService?wsdl
就能看到BookService Web服务的WSDL描述文档
在Spring中集成XFire
删除META-INF/xfire/ 目录,相关信息在Spring的 dispatcher-servlet.xml中定义
使用JSR 181 Web服务注解来标注Web服务
和普通Spring Web程序一样,在web.xml中声明Dispatcher Servlet
在dispatcher-servlet.xml中
注意,这个XML配置文件必须首先通过<import>引入XFire的一些预定义的Bean,
这样就可以直接使用XFireExporter来处理Web服务请求。
此时我们访问WSDL文件的路径为 http://localhost:8080/BookService?wsdl
- public interface BookService {
- Book[] search(String keyword);
- }
- public class BookServiceImpl implements BookService {
- public Book[] search(String keyword) {
- if ("j2ee".equalsIgnoreCase(keyword)) {
- return new Book[] { new Book("Core J2EE Patterns", "Dan Malks"),
- new Book("The J2EE Tutorial", "Bode Carson"),
- new Book("J2EE Design Patterns", "William Crawford"),
- new Book("J2EE Platform Web Services", "Ray Lai") };
- }
- return new Book[0];
- }
- }
- public class Book {
- private String name;
- private String author;
- public Book() {
- }
- public Book(String name, String author) {
- this.name = name;
- this.author = author;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAuthor() {
- return author;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- }
编写XFire必须的Web服务描述文件services.xml
该文件必须放到META-INF/xfire/ 目录下
- <beans>
- <service xmlns="http://xfire.codehaus.org/config/1.0">
- <name>BookService</name>
- <namespace>http://www.livebookstore.net/BookService</namespace>
- <serviceClass>example.chapter8.BookService</serviceClass>
- <implementationClass>example.chapter8.BookServiceImpl</implementationClass>
- </service>
- </beans>
最后,在web.xml中加入
- <web-app>
- <servlet>
- <servlet-name>XFireServlet</servlet-name>
- <servlet-class>
- org.codehaus.xfire.transport.http.XFireConfigurableServlet
- </servlet-class>
- <load-on-startup>0</load-on-startup>
- </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>
- </web-app>
启动服务器,浏览器中输入 http://localhost:8080/services/BookService?wsdl
就能看到BookService Web服务的WSDL描述文档
在Spring中集成XFire
删除META-INF/xfire/ 目录,相关信息在Spring的 dispatcher-servlet.xml中定义
使用JSR 181 Web服务注解来标注Web服务
- @WebService(
- name="BookService",
- serviceName="BookService",
- targetNamespace="http://www.livebookstore.net/BookService"
- )
- public class BookServiceImpl implements BookService {
- @WebMethod
- @WebResult
- public Book[] search(@WebParam String keyword) {
- if("j2ee".equalsIgnoreCase(keyword)) {
- return new Book[] {
- new Book("Core J2EE Patterns", "Dan Malks"),
- new Book("The J2EE Tutorial", "Bode Carson"),
- new Book("J2EE Design Patterns", "William Crawford"),
- new Book("J2EE Platform Web Services", "Ray Lai")
- };
- }
- return new Book[0];
- }
- }
和普通Spring Web程序一样,在web.xml中声明Dispatcher Servlet
- <web-app>
- <servlet>
- <servlet-name>dispatcher</servlet-name>
- <servlet-class>
- org.springframework.web.servlet.DispatcherServlet
- </servlet-class>
- <load-on-startup>0</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>dispatcher</servlet-name>
- <url-pattern>/*</url-pattern>
- </servlet-mapping>
- </web-app>
在dispatcher-servlet.xml中
- <!-- 引用XFire预定义的Bean配置 -->
- <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
- <!-- Web服务实现类 -->
- <bean id="bookService" class="example.chapter8.BookServiceImpl" />
- <!-- 定义URL映射 -->
- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <property name="urlMap">
- <map>
- <entry key="/BookService" value="bookExporter" />
- </map>
- </property>
- </bean>
- <!-- bookExporter将处理来自客户端的调用 -->
- <bean id="bookExporter"
- class="org.codehaus.xfire.spring.remoting.XFireExporter">
- <!-- 对外提供的Web服务的接口 -->
- <property name="serviceClass" value="example.chapter8.BookService" />
- <!-- 实现Web服务的Bean -->
- <property name="serviceBean" ref="bookService" />
- <!-- 下面引用的Bean都已被import引入了 -->
- <property name="serviceFactory" ref="xfire.serviceFactory" />
- <property name="xfire" ref="xfire" />
- </bean>
注意,这个XML配置文件必须首先通过<import>引入XFire的一些预定义的Bean,
这样就可以直接使用XFireExporter来处理Web服务请求。
此时我们访问WSDL文件的路径为 http://localhost:8080/BookService?wsdl