xfire spring web service

  转载

 

 

 

http://hi.baidu.com/lotusxyhf/blog/item/64b5cb52eb667f1e0cf3e3f3.html

  
【整理】用XFire+Spring搭建WebService(Hello简单版+实用版)
2010-11-05 21:34

首先我们需要下载jar包.

如果你有或是正在用MyEclipse的话(我的MyEclipse 版本是5.5.1 GA)就非常容易了。直接新建web项目上右键-->MyEclipse-->Add Web Service Capabilities -->Finish(因为会默认XFIRE 1.2 Core Libraries)

如果你要下载,就去这里:xfire.codehaus.org/Download 下载最新的版本 xfire-distribution-1.2.6.zip (点此直接下载)。

官网还有XFire的源码,日后可以研究使用。

下载下来的zip里面examples-->spring就是一个简单的XFire+Spring的WebService(Echo例子)。

把lib,modules下我们需要的jar包添加到 WEB-INF - lib 目录(Spring的jar包自然要添加进去)。

  1. commons-codec-1.3.jar
  2. commons-httpclient-3.0.jar
  3. commons-logging-1.0.4.jar
  4. jdom-1.0.jar
  5. spring.jar
  6. stax-api-1.0.1.jar
  7. wsdl4j-1.6.1.jar
  8. xfire-all-1.2.6.jar  

先给大家看下我们搭建WebService完后的项目结构图(Eclipse版本)。此外我把这个演示项目连同源码一起发布了一个war包。

下载地址:code.google.com/p/jivam-utils/downloads/list 中的 webservice.war(点此直接下载)。



一、在src下面建立service的包,新建一个接口HelloService。

package service;

public interface HelloService {

public String sayHello(String name);

}

二、建立HelloService的接口实现类HelloServiceImpl。

package service;

public class HelloServiceImpl implements HelloService {

public String sayHello(String name) {
   return "Hello,"+name+" !";
}

}

三、修改WEB-INF下的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml
        classpath:org/codehaus/xfire/spring/xfire.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>xfire</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>xfire</servlet-name>
        <url-pattern>/WebService/*</url-pattern>
    </servlet-mapping>

</web-app>

四、修改WEB-INF下的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="helloBean" class="service.HelloServiceImpl"/>
</beans>

五、在WEB-INF下新建xfire-servlet.xml。(要结合上面的各个xml理解其含义,其实很简单)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="urlMap">
            <map>
                <entry key="/HelloService">
                    <ref bean="hello"/>
                </entry>
            </map>
        </property>
    </bean>
   
    <bean id="hello" class="org.codehaus.xfire.spring.remoting.XFireExporter">
        <property name="serviceFactory">
            <ref bean="xfire.serviceFactory"/>
        </property>
        <property name="xfire">
            <ref bean="xfire"/>
        </property>
        <property name="serviceBean">
            <ref bean="helloBean"/>
        </property>
        <property name="serviceClass">
            <value>service.HelloService</value>
        </property>
    </bean>

</beans>

这时候WebService就搭建完成了,那怎么测试呢?

请点击http://localhost:8080/webservice/WebService/HelloService?wsdl

看到下面的情况你就成功了:

<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions targetNamespace="http://service" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:tns="http://service" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
- <wsdl:types>
- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://service">
- <xsd:element name="sayHello">
- <xsd:complexType>
- <xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
- <xsd:element name="sayHelloResponse">
- <xsd:complexType>
- <xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
- <wsdl:message name="sayHelloRequest">
<wsdl:part name="parameters" element="tns:sayHello" />
</wsdl:message>
- <wsdl:message name="sayHelloResponse">
<wsdl:part name="parameters" element="tns:sayHelloResponse" />
</wsdl:message>
- <wsdl:portType name="HelloServicePortType">
- <wsdl:operation name="sayHello">
<wsdl:input name="sayHelloRequest" message="tns:sayHelloRequest" />
<wsdl:output name="sayHelloResponse" message="tns:sayHelloResponse" />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="HelloServiceHttpBinding" type="tns:HelloServicePortType">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="sayHello">
<wsdlsoap:operation soapAction="" />
- <wsdl:input name="sayHelloRequest">
<wsdlsoap:body use="literal" />
</wsdl:input>
- <wsdl:output name="sayHelloResponse">
<wsdlsoap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
+ <wsdl:service name="HelloService">
- <wsdl:port name="HelloServiceHttpPort" binding="tns:HelloServiceHttpBinding">
<wsdlsoap:address location="http://localhost:8080/webservice/WebService/HelloService" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

那下面我们来写个客户端(客户端需要commons-httpclient-3.0.jar,lib里面就有)。src下面新建test目录,新建HelloClient ,因为客户端和服务器端我写在同样的一个项目里,所以 HelloService 我直接引用了,实际项目中是分开的2个项目,此时就需要服务器的项目吧 HelloService 接口,如果有交互的实体类,则连同接口一起打成JAR包,放到客户端的目录下,然后就可以引用了。

 

 

 

package test;

import java.net.MalformedURLException;

import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;

import service.HelloService;

public class HelloClient{

public static void main(String[] args) {
   Service srvcModel = new ObjectServiceFactory().create(HelloService.class);
   XFire xfire = XFireFactory.newInstance().getXFire();
   XFireProxyFactory factory = new XFireProxyFactory(xfire);
   String serviceUrl = "http://localhost:8080/webservice/WebService/HelloService";
   HelloService hs = null;
    try {
     hs = (HelloService)factory.create(srvcModel, serviceUrl);
    } catch (MalformedURLException e) {
     e.printStackTrace();
    }
    System.out.println(hs.sayHello("Jivam"));
}
}

=========项目过滤器中怎么安全的过滤WebService请求========

如果web.xml中 xFire的url-pattern地址是 <url-pattern>/WebService/*</url-pattern>

那么在相应的Filter中应这样过滤才是安全的:

HttpServletRequest req = (HttpServletRequest)request;
if(req.getServletPath().equals("/WebService")){
        chain.doFilter(request, response);
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值