cxf发布webservice

来源:互联网 发布:js windows调用firefox 编辑:程序博客网 时间:2024/05/16 10:23

准备: 新建工程 导入需要的jar 包:

                  

                  依赖的包:

                            commons-logging-1.1.jar
                            geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar)
                            geronimo-annotation_1.0_spec-1.1.jar (JSR 250)
                            geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar)
                            geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar)
                            geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
                            jaxb-api-2.1.jar
                            jaxb-impl-2.1.6.jar
                            jaxws-api-2.1.jar
                            jetty-6.1.5.jar
                            jetty-util-6.1.5.jar
                            neethi-2.0.jar
                            saaj-api-1.3.jar
                            saaj-impl-1.3.jar
                            stax-api-1.0.1.jar
                            wsdl4j-1.6.1.jar
                            wstx-asl-3.2.1.jar
                            XmlSchema-1.2.jar
                            xml-resolver-1.2.jar     

 

                  spring jar 包, 用来支持xml配置:

                            aopalliance-1.0.jar
                            spring-core-2.0.4.jar
                            spring-beans-2.0.4.jar
                            spring-context-2.0.4.jar
                            spring-web-2.0.4.jar

                  

                   CXF jar包:

                            cxf-2.1.jar

   

         以上jar 包 可从apache官方网站下载 apache-cxf-2.1.2.zip, 然后从apache-cxf-2.1.2/lib 目录中获得

 

      1  首先服务点接口。

          package com.demo;

 

          import java.util.List;

          import javax.jws.WebParam;
          import javax.jws.WebService;
         

          @WebService
          public interface HelloWorld {
               String sayHi(@WebParam(name="text")String text);
               String sayHiToUser(User user);
               String[] SayHiToUserList(List<User> userList);
           }

 

     2  编写服务实现

         package com.demo;

         import java.util.LinkedHashMap;
         import java.util.List;
         import java.util.Map;

         import javax.jws.WebService;

 

         @WebService(endpointInterface="com.demo.HelloWorld",serviceName="HelloWorld")
         public class HelloWorldImpl implements HelloWorld {
 
                    Map<Integer, User> users = new LinkedHashMap<Integer, User>();

 
                    public String sayHi(String text) {
                                return "Hello " + text;
                   }

 

                   public String sayHiToUser(User user) {
                             users.put(users.size()+1, user);
                             return "Hello "+ user.getName();
                   }

                   public String[] SayHiToUserList(List<User> userList) {
                             String[] result = new String[userList.size()];
                             int i=0;
                             for(User u:userList){
                                  result[i] = "Hello " + u.getName();
                                  i++;
                             }
                     return result;
                   }
       }

 

  3  编写 webServiceApp.java类来暴露 web服务。

      package com.demo;

      import javax.xml.ws.Endpoint;

 

      public class webServiceApp {
                 public static void main(String[] args) {
                           System.out.println("web service start");
                           HelloWorldImpl implementor= new HelloWorldImpl();
                           String address="
http://localhost:8080/helloWorld";
                           Endpoint.publish(address, implementor);
                           System.out.println("web service started");
                  }
      }

 4  run webServiceApp.java 类来启动服务。 访问 http://localhost:8080/helloWorld?wsdl  查看是否显示

     wsdl。

  

 5  编写客户端访问服务。

     package com.demo;

     import java.util.ArrayList;
     import java.util.List;

     import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
     import org.springframework.context.support.ClassPathXmlApplicationContext;

 

     public class HelloWorldClient {
              public static void main(String[] args) {
                        JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
                        svr.setServiceClass(HelloWorld.class);
                        svr.setAddress("
http://localhost:8080/helloWorld");
                        HelloWorld hw = (HelloWorld) svr.create();
                        User user = new User();
                        user.setName("Tony");
                        user.setDescription("test");
                        System.out.println(hw.sayHiToUser(user));
              }
     }

 

 

 

  6  测试: run webServiceApp.java 类来启动服务,然后 run HelloWorldClient.java 来访问服务。

 

  二 集成到spring 中。

  

  1 在 web.xml 中加入 :

 

       <?xml version="1.0" encoding="UTF-8"?>
       <web-app>
                <welcome-file-list>
                         <welcome-file>index.jsp</welcome-file>
                </welcome-file-list>


                <context-param>
                          <param-name>contextConfigLocation</param-name>
                          <param-value>WEB-INF/classes/applicationContext.xml</param-value>
                 </context-param>
 
              <listener>
                      <listener-class>
                              org.springframework.web.context.ContextLoaderListener
                      </listener-class>
              </listener>

 

              <servlet>
                     <servlet-name>CXFServlet</servlet-name>
                     <display-name>CXFServlet</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>/webservice/*</url-pattern>
               </servlet-mapping>
         </web-app>

 

2  在 applicationContext.xml 中加入:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="
http://www.springframework.org/schema/beans"
                 xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:jaxws="
http://cxf.apache.org/jaxws"
                 xsi:schemaLocation="
                       
http://www.springframework.org/schema/beans

                       http://www.springframework.org/schema/beans/spring-beans.xsd
                       http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
 
                <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"/>
 
                 <jaxws:endpoint 
                              id="helloWorld"
                              implementor="com.demo.HelloWorldImpl"
                              address="/helloWorld" />
   
                <bean id="client" class="com.demo.HelloWorld" 
                           factory-bean="clientFactory" factory-method="create"/>
    
                 <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
                            <property name="serviceClass" value="com.demo.HelloWorld"/>
                            <property name="address"

                                              value="http://localhost:8080/s/webservice/helloWorld"/>
                  </bean>
     </beans>

 

    注意: 这里需要加入  xmlns:jaxws="http://cxf.apache.org/jaxws"   

              http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd

 

  3  修改客户端。

      package com.demo;

     import java.util.ArrayList;
     import java.util.List;

     import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
     import org.springframework.context.support.ClassPathXmlApplicationContext;

 

     public class HelloWorldClient {
              public static void main(String[] args) {
                        HelloWorld client = (HelloWorld)context.getBean("client");
                        User user1 = new User();
                        user1.setName("Tony");
                        user1.setDescription("test");
                        User user2 = new User();
                        user2.setName("freeman");
                        user2.setDescription("test");
                        List<User> userList= new ArrayList<User>();
                        userList.add(user1);
                        userList.add(user2);
                        String[] res = client.SayHiToUserList(userList);
                        System.out.println(res[0]);
                        System.out.println(res[1]);          

              }
     }

 

  4  发布工程 启动web服务器(我用 tomcat 6)。

 

  5 访问 http://localhost:8080/s/webservice/helloWorld?wsdl  查看是否显示 wsdl 。

 

  6  run run HelloWorldClient.java 来访问服务。
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 佐佐木明希 房东中文字幕 与父亲干柴烈火 中文字幕丈夫不在 9大嫂被禽翻中文字幕 侵犯你的贞洁 中文字幕 年轻的妻子在线观中文字幕 大嫂被翻天了佐佐木b希中文7 美丽的大嫂中文字幕影迅雷下载 邻居的妻子中文字幕下载 神马电影院电影中文 神马电影院理论中文 女儿的朋友5中文神马电影院 97手机2019电影院专用版中文 厨房里进入朋友的老婆 中文版电影院 神马电影院 中文 儿子的妻子中文字幕 下载 樱桃中文版电影院 大富豪电影院韩国中文 老婆的闺蜜们喝醉了在家 中文潮人影院您手中的电影院 朋友不在晚上去他家干 趁兄弟喝醉上他女朋友在线播放 神马电影院午伦中文 朋友喝醉上其妻 我朋友的妻子韩语中文2018 在朋友家趁朋友喝醉上他老婆 日本朋友的妻子和母亲中文版 偷朋友的妻子在线中文播放 邻居的妻子日本中文2018 朋友的妻子日本中文版7 朋友的妈l妈中文字电影 母亲在美国被黑人证服 妻子报恩献身张局长加强版2 日本朋友的家教妻子中文字幕 妻陪领导睡全文阅读 看着妻子被领导玩电影 看着领导糟蹋妻子 带妻子群 p 原千岁的牛仔裤剪开裆 俩黑人夹击得我直嗤尿液故事