spring Web Service的2种实现方式

实现web service服务端主要有4步:

1、准备工作

      创建接口,实现类,实体类

2、配置web.xml

3、配置applicationContext.xml

4、访问服务端的wsdl文件

服务端

1.1准备工作

1.       一个model类,Message 如:

    publicclass Message

{

       public Message(String userid, String title, String msg)

        {

           super();

           this.userid = userid;

           this.title = title;

           this.msg = msg;

        }

 

       public Message()

        {

           super();

        }

        private String userid;

        private String title;

        private String msg;

}

属性的setget方法自行实现

 

2.       一个接口

@WebService(name="helloWorld",targetNamespace="http://com.wb")

publicinterface IHelloWorld

{

           /**

           * 获取信息内容

           * @param title 标题

           * @return String

           * */

           @WebMethod(operationName="getMsg",action="")

           @WebResult(name="out",targetNamespace="http://com.wb")

           public String getMsg(@WebParam(name="title")String title);

   

           /**

           * @param userId 用户id

           * @param title 标题

           * @return Message

           * */

           @WebMethod(operationName="getMessageByUserid",action="")

           @WebResult(name="obj",targetNamespace="http://com.wb")

           public Message getMessageByUserid(

                  @WebParam(name="userId")String userId,

                  @WebParam(name="title")String title);

   

   

           /**

           * @param userId 用户id

           * @return Message[]

           * */

           @WebMethod(operationName="getMessages",action="")

          @WebResult(name="arr",targetNamespace="http://com.wb")

       public Message[] getMessages(@WebParam(name="userId")String userId);

   

}

说明:@WebService name 表示service的名称 targetNamespace表示命名空间

@WebMethod operationName 表示方法名称

@WebResult name表示返回值名称,targetNamespace表示命名空间,此命名空间跟service的命名空间一致

3.       一个接口实现类

publicclass HelloWorldImpl implements IHelloWorld

{

           @Override

           public String getMsg(String title)

           {

              return"hello!";

           }

 

           @Override

           public Message getMessageByUserid(String userId, String title)

           {

             Message mes=new Message("test","test xfire webService","lxl");

                  return mes;

           }

 

           @Override

           public Message[] getMessages(String userId)

           {

              Message mes=new Message("test","test xfire webService","lxl");

               Message mes1=new Message("test1","test xfire webService","lxl");

              Message mes2=new Message("test2","test xfirewebService","lxl");

      

              Message[] meses=new Message[3];

              meses[0]=mes;

              meses[1]=mes1;

              meses[2]=mes2;

              return meses;

           }

}

1.2web.xml配置

1.基于xfire方式的配置

<listener>        

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>  

   </listener>

  

    <context-param>  

        <param-name>contextConfigLocation</param-name>  

            <param-value>

/WEB-INF/classes/applicationContex*.xml

</param-value>  

    </context-param>  

  

   <servlet>  

<servlet-name>xfire</servlet-name>  

        <servlet-class>

org.codehaus.xfire.spring.XFireSpringServlet

</servlet-class>  

        <load-on-startup>1</load-on-startup>  

   </servlet>  

 

<servlet-mapping>  

        <servlet-name>xfire</servlet-name>  

        <url-pattern>/*</url-pattern>  

    </servlet-mapping> 

2.基于cxf方式的配置

  <listener>  

         <listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>  

   </listener>

  

  <context-param>  

        <param-name>contextConfigLocation</param-name>  

        <param-value>/WEB-INF/classes/applicationContex*.xml</param-value>  

   </context-param>  

  

   <servlet>  

        <servlet-name>CXFServlet</servlet-name>  

        <display-name>CXF Servlet</display-name>  

<servlet-class>

org.apache.cxf.transport.servlet.CXFServlet

</servlet-class>  

        <load-on-startup>1</load-on-startup>  

    </servlet>  

 

    <servlet-mapping>  

        <servlet-name>CXFServlet</servlet-name>  

        <url-pattern>/*</url-pattern>  

</servlet-mapping>

1.3applicationContext.xml配置

1.基于xfire方式的配置

    <!—引入xfire.xml -->

<import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>

    <bean id="configImpl" class="com.wb.HelloWorldImpl"/>

    <!--发布webService接口 -->

    <bean id="getConfigService" class="org.codehaus.xfire.spring.ServiceBean">

<!—注入接口的实现类-->

        <property name="serviceBean" ref="configImpl"></property>

<!—注入接口-->

        <property name="serviceClass" value="com.wb.IHelloWorld">

</property>

        <property name="inHandlers">

                   <list>

                       <ref bean="addressingHandler"/>

                   </list>

         </property>

     </bean> 

   

      <bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler">

</bean>

 

2.基于cxf方式的配置

   <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" />

   

    <bean id="hello" class="com.wb.HelloWorldImpl"/>

   

<jaxws:endpoint id="helloService" implementor="#hello" address="/HelloWorld"></jaxws:endpoint>

 

1.4访问服务端的wsdl文件

服务端配置到此已完成,启动项目

 

如果在web.xml中配置serverlet的访问路径是/*,则访问路径是http:ip:端口/项目名称/

1.xfire方式的访问

具体访问到某个接口的wsdl路径:http:ip:端口/项目名称/接口名称?wsdl

2.基于cxf方式的访问

         具体访问到某个接口的wsdl路径:http:ip:端口/项目名称/接口访问路径?wsdl

注:该接口访问路径指的是在applicationContext.xml文件中配置的jaxws:endpoint标签的

Address属性。

 

客户端

准备工作

1. 新建项目 , 添加 xfire

2. 创建modelMessage,属性跟服务端的Message一样,在此不再赘述

3. 根据wsdl文件创建接口,接口跟服务端的一样

基于xfire方式调用

   publicclass Test

{

        publicstaticvoid main(String args[]){

              Test test=new Test();      

              IHelloWorld hello=test.getObj();

              System.out.println(hello.getMsg("aaa"));

              Message mes=hello.getMessageByUserid("lxl","test");

System.out.println("标题:"+mes.getTitle()+",内容:"+mes.getMsg()+",创建者:"+mes.getUserid());

              System.out.println("获取用户的所有信息");

              Message[] meses=hello.getMessages("lxl");

              for(Message m: meses){

                      System.out.println("标题:"+m.getTitle()+",内容:"+m.getMsg()+",创建者:"+m.getUserid());

              }

        }

   

        public IHelloWorld getObj(){

Service serviceModel = new ObjectServiceFactory().create(IHelloWorld.class);

// Create a proxy for the deployed service XFire获得一个代理工厂对象

              XFire xfire = XFireFactory.newInstance().getXFire();

              XFireProxyFactory factory = new XFireProxyFactory(xfire);

              // 得到一个服务的本地代理

String serviceUrl = "http://localhost:8080/webwsxifre/IHelloWorld";

              IHelloWorld client = null;

              try

              {

client = (IHelloWorld) factory.create(serviceModel, serviceUrl);

              }

              catch(MalformedURLException e)

              {

                   System.out.println("WsClient.callWebService(): EXCEPTION: " + e.toString());

              }  

                  return client;

            }

        }

基于cxf方式调用

publicclass Test

{

    publicstaticvoid main(String args[]){

      

       Test test=new Test();      

       IHelloWorld hello=test.getObj();

      

       System.out.println(hello.getMsg("aaa"));

       Message mes=hello.getMessageByUserid("lxl","test");

       System.out.println("标题:"+mes.getTitle()+",内容:"+mes.getMsg()+",创建者:"+mes.getUserid());

       System.out.println("获取用户的所有信息");

       Message[] meses=hello.getMessages("lxl");

       for(Message m: meses){

           System.out.println("标题:"+m.getTitle()+",内容:"+m.getMsg()+",创建者:"+m.getUserid());

       }

    }

    public IHelloWorld getObj(){

       // Create a proxy for the deployed service 获得一个代理工厂对象

       JaxWsProxyFactoryBean clientFactory=new JaxWsProxyFactoryBean();     

       clientFactory.setServiceClass(IHelloWorld.class);

       clientFactory.setAddress("http://localhost:8080/webwscxf/HelloWorld");

       IHelloWorld client=(IHelloWorld)clientFactory.create();

       return client;

    }

}

注意:客户端接口的命名空间一定要跟服务端的命名空间一致

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值