Webservice

什么是Webservice?

概念:http://baike.baidu.com/link?url=T7_zhfRHOeBVp4nc_y2lXg6Gf2MQVQyDNQo98Ay461XP61OhIZPmNt6eCU0LO5vwZfn2-guc1XKWlxyz_TAQXS6uVtNvDnGDK_oxYi8W-eA9TW8ZarrfnzvRUa5AcIKofD-zBZuUptWmbr-veqW_tBgPFznsR2R9sTlN-50uLzm
通俗理解:http://www.onmpw.com/tm/xwzj/network_45.html

Webservice的理解关键是XML、WSDL、SOAP、UDDI这几个是什么。

比较常用的Webservice框架

Apache CXF、Axis两款。

CXF框架实战

Demo源码:https://github.com/Huangdongrong/cxf-demo/tree/master

服务端

基于CXF+Spring整合,实现CXF服务端

  • 接口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    package com.demo.service;
    import javax.jws.WebParam;
    import javax.jws.WebService;
    import javax.jws.soap.SOAPBinding;
    import javax.jws.soap.SOAPBinding.Style;
    @SuppressWarnings("restriction")
    @WebService
    @SOAPBinding(style=Style.RPC)
    public interface IHelloWorld {
    public String sayHello(@WebParam(name = "name") String name);
    }
  • 实现类

    1
    2
    3
    4
    5
    6
    7
    package com.demo.service;
    public class HelloWorldImpl implements IHelloWorld{
    public String sayHello(String name) {
    return name + " say: Hello World!";
    }
    }
  • Spring配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:cxf="http://cxf.apache.org/core"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
    http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    ">
    <context:component-scan base-package="com" />
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    <!--访问地地址http://localhost:8080/webservice-demo/helloWorld?wsdl -->
    <!-- 服务器端 -->
    <bean id="helloService" class="com.demo.service.HelloWorldImpl" />
    <jaxws:server id="helloWebService"
    serviceClass="com.demo.service.IHelloWorld"
    address="/helloWorld">
    <!-- 要暴露的 bean 的引用,上面定义的bean id -->
    <jaxws:serviceBean>
    <ref bean="helloService"/>
    </jaxws:serviceBean>
    </jaxws:server>
    </beans>
  • web.xml配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>Archetype Created Web Application</display-name>
    <!-- Spring配置文件开始 -->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    classpath:spring/applicationContext.xml,
    classpath*:/Cxf-config.xml
    </param-value>
    </context-param>
    <context-param>
    <param-name>spring.profiles.default</param-name>
    <param-value>development</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
    <servlet-name>CXFServlet</servlet-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>
    <filter>
    <filter-name>characterFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>characterFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
    </web-app>

客户端

  1. 下载Binary distribution包apache-cxf-3.1.8.zip:http://cxf.apache.org/download.html,解压
  2. 打开控制台进入cxf下的bin目录

    1
    2
    C:\Users\Donie>cd C:\Users\Donie\Downloads\apache-cxf-3.1.8\apache-cxf-3.1.8\bin
    C:\Users\Donie\Downloads\apache-cxf-3.1.8\apache-cxf-3.1.8\bin>C:
  3. 根据命令生成客户端

    1
    C:\Users\Donie\Downloads\apache-cxf-3.1.8\apache-cxf-3.1.8\bin>wsdl2java -verbose -frontend jaxws21 -client -encoding utf-8 C:\Users\Donie\Downloads\apache-cxf-3.1.8\apache-cxf-3.1.8\helloWorld.wsdl
  4. 生成客户端java文件在cxf下的bin目录

  5. 客户端调用

  • 使用本地方式调用

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    package com.demo.service.localwsdl;
    import java.io.File;
    import java.net.MalformedURLException;
    import java.net.URL;
    import javax.xml.namespace.QName;
    import javax.xml.ws.BindingProvider;
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebResult;
    import javax.jws.WebService;
    import javax.jws.soap.SOAPBinding;
    public final class IHelloWorld_IHelloWorldPort_Client {
    private static final QName SERVICE_NAME = new QName("http://service.demo.com/", "IHelloWorldService");
    private IHelloWorld_IHelloWorldPort_Client() {
    }
    public static void main(String args[]) throws java.lang.Exception {
    //两种写法
    /* URL wsdlURL = IHelloWorldService.WSDL_LOCATION;
    IHelloWorldService ss = new IHelloWorldService(wsdlURL, SERVICE_NAME);
    IHelloWorld port = ss.getIHelloWorldPort();
    System.out.println("Invoking sayHello...");
    java.lang.String _sayHello_name = "dd";
    java.lang.String _sayHello__return = port.sayHello(_sayHello_name);
    System.out.println("sayHello.result=" + _sayHello__return);*/
    /* Set NEW Endpoint Location */ //JAX-WS的写法
    IHelloWorldService service = new IHelloWorldService();
    IHelloWorld port = service.getIHelloWorldPort();
    String endpointURL = "http://localhost:8080/webservice-demo/helloWorld";
    BindingProvider bp = (BindingProvider)port;
    bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointURL);
    java.lang.String _sayHello_name = "xx";
    System.out.println( port.sayHello(_sayHello_name));
    }
    }
  • 与Spring整合调用
    1.Spring文件增加配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:cxf="http://cxf.apache.org/core"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
    http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    ">
    <jaxws:client id="helloServiceClient"
    serviceClass="com.demo.service.IHelloWorld"
    address="http://localhost:8080/webservice-demo/helloWorld"/>
    </beans>
  1. 调用如普通Service
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    package com.demo.service;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class HelloServiceClient {
    public static void main(String[] args) {
    // 加载客户端的配置定义
    ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
    // 获取定义的 Web Service Bean
    IHelloWorld helloService = (IHelloWorld) context.getBean("helloServiceClient");
    String username = "Ailan";
    // 调用方法进行服务消费
    String result = helloService.sayHello(username);
    System.out.println("Result:" + result);
    }
    }

Axis实战实战

http://clq9761.iteye.com/blog/976029/

Webservice测试软件->SoapUI

SoapUI下载:http://www.xiazaiba.com/html/82278.html
SoapUI使用:http://blg.csdn.net/russ44/article/details/51680083

文章目录
  1. 1. 什么是Webservice?
  2. 2. 比较常用的Webservice框架
  3. 3. CXF框架实战
    1. 3.1. 服务端
    2. 3.2. 客户端
  4. 4. Axis实战实战
  5. 5. Webservice测试软件->SoapUI
,