webservice简单运用_笔记

1、导入cxf的jar包;

2、在项目的web.xml中配置cxf如下:

    <!-- CXF -->
    <servlet>   
        <servlet-name>CXFService</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    <servlet-mapping>   
        <servlet-name>CXFService</servlet-name>   
        <url-pattern>/cxfService/*</url-pattern>
    </servlet-mapping>

3、编写代码

    3-1:接口HelloWorldService

        public interface HelloWorldService {

            void sayHello(String str);
        }

    3-2:实现类HelloWorldServiceImpl

        import javax.jws.WebService;

        import cn.ce.smap.webservice.HelloWorldService;
        @WebService
        public class HelloWorldServiceImpl implements HelloWorldService {

            @Override
            public void sayHello(String str) {
                System.out.println(str + ",你好!");
            }

        }
    3-3:测试类HelloWorldServiceTest
        import org.apache.cxf.endpoint.Client;
        import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
        import org.apache.cxf.transport.http.HTTPConduit;
        import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

        public class HelloWorldServiceTest {

            public static void main(String[] args) throws Exception {
                 String WSDL_URL_QC = "http://localhost:8080/smaDev/cxfService/HelloService?wsdl";
                JaxWsDynamicClientFactory wcf = JaxWsDynamicClientFactory.newInstance();
                Client client = wcf.createClient(WSDL_URL_QC);
                // 设置超时单位为毫秒
                HTTPConduit http = (HTTPConduit) client.getConduit();
                HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
                httpClientPolicy.setConnectionTimeout(60000);// 连接超时600s
                // httpClientPolicy.setAllowChunking(false);//取消块编码
                httpClientPolicy.setReceiveTimeout(6000000);// 响应超时600s
                http.setClient(httpClientPolicy);
                Object[] results = client.invoke("sayHello", "test");
                System.out.println(results.length);
            }
        }

    3-4:配置service.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"
        xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                       http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd
                       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
                       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
                       http://cxf.apache.org/jaxws
                       http://cxf.apache.org/schemas/jaxws.xsd
                       http://cxf.apache.org/transports/http/configuration
                          http://cxf.apache.org/schemas/configuration/http-conf.xsd"
        default-autowire="byName" default-lazy-init="false">
        <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 name="helloWorldService" class="cn.test.HelloWorldService"/>
        <jaxws:endpoint id="HelloService" implementor="#helloWorldService"

             address="/helloWorldService" />
        <http-conf:conduit name="*.http-conduit">     
            <http-conf:client ConnectionTimeout="10000" ReceiveTimeout="1800000"/>    
        </http-conf:conduit>   
    </beans>

 

最后运行测试类就可以了。

 

你可能感兴趣的:(webservice)