zoukankan      html  css  js  c++  java
  • 使用cxf写web service的简单实例

    实例步骤:

    第一步:在myeclipse中新建一个web项目名为webservicetest,并导入依赖的jar包(cxf,spring,apache-commons相关)
    commons-logging-1.0.4.jar
    cxf-2.6.2.jar
    geronimo-jaxws_2.2_spec-1.0.jar
    geronimo-ws-metadata_2.0_spec-1.1.3.jar
    neethi-3.0.2.jar
    cxf结合spring时所需jar包,此例子也需要这些,用到了spring上下文加载:
    spring-asm-3.0.7.RELEASE.jar
    spring-beans-3.0.7.RELEASE.jar
    spring-context-3.0.7.RELEASE.jar
    spring-core-3.0.7.RELEASE.jar
    spring-expression-3.0.7.RELEASE.jar
    spring-aop-3.0.7.RELEASE.jar
    spring-web-3.0.7.RELEASE.jar


    xmlschema-core-2.0.3.jar
    jaxb-api-2.2.6.jar
    wsdl4j-1.6.1.jar
    jsr173_api-1.0.jar
    common-annotations.jar
    jaxb-impl-2.0.1.jar
    stax-api-1.0.0.jar
    wstx-asl-3.2.0.jar
    jetty-util-7.5.4.v20111024.jar
    jetty-continuation-7.5.4.v20111024.jar
    jetty-http-7.5.4.v20111024.jar
    jetty-io-7.5.4.v20111024.jar
    jetty-security-7.5.4.v20111024.jar
    jetty-server-7.5.4.v20111024.jar

    @WebService和@WebMethod是WSDL映射Annotation.这些Annotation将描述Web Service的WSDL文档元素和JAVA源代码联系在一起。
    @WebService annotation的元素name,serviceName和targetNamespace成员用来描述wsdl:protType,wsdl:service,和targetNameSpace生成WebService中的WSDL文件。
    @SOAPBinding是一个绑定的annotation,用来说明网络协议和格式
    @SOAPBinding是一个用来描述SOAP格式和RPC的协议的绑定Annotation.
    @WebMethod Annotation的operationName成员描述了wsdl:operation,而且它的操作描述了WSDL文件中的SOAPAction头部,这是客户端必须要放入到SOAPHeader中的数值,SOAP1.1中的一种约束。
    @WebParam Annotation的partName成员描述了WSDL文档中的wsdl:part.
    @WebResult Annotation的partName成员描述了wsdl:part用来返回WSDL文档的值。

    第二步:创建webservice接口及实现类(下面为Java代码)
    创建HelloWorld接口

    View Code

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

    第三步:创建HelloWorldImpl实现类

    View Code
    @WebService(endpointInterface = "demo.spring.service.HelloWorld", serviceName = "HelloWorld")
    //endpointInterface表示该类就必须实现此接口所有方法,serviceName表示webservice的服务名称
    public class HelloWorldImpl implements HelloWorld {
        Map<Integer, User> users = new LinkedHashMap<Integer, User>();
        @WebMethod
        public String sayHi(String text) {
            return "Hello " + text;
        }
        @WebMethod
        public String sayHiToUser(User user) {
            users.put(users.size() + 1, user);
            return "Hello " + user.getName();
        }
        @WebMethod
        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;
        }
    }

    第四步:启动webserviceApp.java,访问http://localhost:8080/helloWorld?WSDL

    View Code

    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");
                }
    }

    第五步:在web.xml中加入cxf相应配置,内容如下:

    View Code

    <!--cxf start-->  
    <!--用于加载applicationContext.xml配置信息-->  
        <context-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>WEB-INF/classes/applicationContext.xml</param-value>  
        </context-param>  
        <!--使用spring ContextLoaderListener 加载applicationContext.xml-->  
        <listener>  
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
        </listener>  
    <!--配置CXFServlet-->  
        <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可自定义配置,用于CXFServlet请求地址拦截,访问会用到 -->  
            <url-pattern>/webservice/*</url-pattern>  
        </servlet-mapping>  
    <!--cxf end -->  

    第六步:在src中创建基本的applicationContext.xml内容如下(作用:主要做webservice接口属性配置,通过web.xml配置加载)

    View Code

    <?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" />
    
    <!--id:名称(随意配),implementor:指定接口具体实现类,address:随意配-->  
        <jaxws:endpoint id="helloWorld"
            implementor="demo.spring.service.HelloWorldImpl"
            address="/HelloWorld" /> 
    
    <!-- WebService 客户端 spring 配置文件
    cxf与Spring集成,cxf里提供了一个工厂类org.apache.cxf.jaxws.JaxWsProxyFactoryBean,可以方便实现的调用WebService。
    serviceClass属性是接口类,address是webService的路径
    在其他bean里如果要调用webservice,只要将client这个bean注入到需要使用的bean里。
    -->
        <bean id="client" class="demo.spring.service.HelloWorld"
            factory-bean="clientFactory" factory-method="create" />
    
        <bean id="clientFactory"
            class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
            <property name="serviceClass"
                value="demo.spring.service.HelloWorld" />
            <property name="address"
                value="http://localhost:8080/webservices/HelloWorld" />
        </bean>
    </beans>

    第七步:发布webservice到tomcat

    Java代码,验证WSDL是否发布成功:
    如果项目发布放在/webapps/ROOT下时:
    访问http://IP地址:端口/webservices/{applicationContent.xml中配置的address}?wsdl
    //webservices对应web.xml中的<url-pattern>/webservices/*</url-pattern>验证是否能正常看到xml格式的页面

    第八步:测试

    View Code

    public class HelloWorldClient {
        public static void main(String[] args) {
            /**
             * 简单的用ApplicationContext做测试的话,获得Spring中定义的Bean实例(对象),可以用以下方法
             * ClassPathXmlApplicationContext[只能读放在web-info/classes目录下的配置文件]
            */
            ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
            HelloWorld client = (HelloWorld) context.getBean("client");
            
            User user = new User();
            user.setName("Tony");
            user.setDescription("test");
            
            String str = client.sayHiToUser(user);
            System.out.println(str);
        }
    }

     

  • 相关阅读:
    (转载)敏捷开发一千零一问系列之三十三:每天编码多少行?
    (转载)memcpy的几个实现版本
    (转载)HTTP URL
    (转载)C++lambda表达式
    (转载)程序员面试什么最重要?
    (转载)static全局变量与普通的全局变量有什么区别?
    (转载)【C++】new A和new A()的区别详解
    (转载)php如何判断IP为有效IP地址
    查看修复HDFS中丢失的块
    dfs.replication、dfs.replication.min/max及dfs.safemode.threshold.pct
  • 原文地址:https://www.cnblogs.com/sfeng1825/p/2695628.html
Copyright © 2011-2022 走看看