Web Service系列(三)使用Apache CXF实现jax-ws规范

Apache CXF

Web Service的使用

  • java有提供webservice规范、实现支持。

    可参考文档

  • 一般在开发时候,都是使用一些框架进行webservice开发。例如:ApacheCXF

WebService框架的性能测试 : https://blog.csdn.net/ck4438707/article/details/53610124

Apache CXF使用WebService(jax-ws规范)

独立程序的使用

服务端

  1. 添加依赖
    • 如果是一个独立程序,比如jar,就需要内置服务器
    • 注意使用webService,jre环境必须要是1.5以上
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.Kato</groupId>
  <artifactId>jaxWs_server</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>服务端</name>

  <!-- 添加CXF依赖 -->
    <dependencies>
  <!-- 要进行jaxws 服务开发 -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.0.1</version>
        </dependency>

        <!-- 内置jetty web服务器  -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.0.1</version>
        </dependency>

        <!-- 日志实现 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.12</version>
        </dependency>
  </dependencies>

  <build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>

        </plugins>
    </pluginManagement>
  </build>

</project>
  1. 书写接口和实体类

    • 接口要添加注解@WebService

    • 接口

/**
 * 服务端接口
 */
@WebService
public interface UserServer {

    public String welcome(String name);
}
  • 实现
/**
 * 服务端实现 
 */
public class UserServerImpl implements UserServer{

    @Override
    public String welcome(String name) {
        return name+",welcome!!";
    }
}
  1. 开启服务
/**
 * 开启服务
 */
public class APP {
    public static void main(String[] args) {
        //使用jdk发布服务
        //Endpoint.publish("http://127.0.0.1:9999/Hello", new UserServerImpl());

        //使用CXF 提供的API 发布服务
        //创建服务工厂
        JaxWsServerFactoryBean jax = new JaxWsServerFactoryBean();

        //服务地址
        jax.setAddress("http://127.0.0.1:9999/Hello");

        //设置服务类
        jax.setServiceClass(UserServerImpl.class);
//      jax.setServiceBean(new UserServerImpl());

        //添加日志拦截器,观察soap协议内容
        jax.getInInterceptors().add(new LoggingInInterceptor());
        jax.getOutInterceptors().add(new LoggingOutInterceptor());

        //发布服务
        jax.create();
        System.out.println("服务开启成功!!端口号:9999");
    }
}
  1. 打开address的wsdl

客户端

  1. 添加依赖
    • 如果是一个独立程序,比如jar,就需要内置服务器
    • 注意使用webService,jre环境必须要是1.5以上
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.Kato</groupId>
  <artifactId>jaxWs_server</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>服务端</name>

  <!-- 添加CXF依赖 -->
    <dependencies>
  <!-- 要进行jaxws 服务开发 -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.0.1</version>
        </dependency>

        <!-- 内置jetty web服务器  -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.0.1</version>
        </dependency>

        <!-- 日志实现 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.12</version>
        </dependency>
  </dependencies>

  <build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>

        </plugins>
    </pluginManagement>
  </build>

</project>
  1. 书写接口和实体类

    • 接口要添加注解@WebService

    • 接口

/**
 * 服务端接口
 */
@WebService
public interface UserServer {

    public String welcome(String name);
}
  1. 远程调用服务
/**
 * 客户端
 */
public class Client {

    /**
     * 远程调用服务
     */
    public static void main(String[] args) {
        //服务代理工厂
        JaxWsProxyFactoryBean jax = new JaxWsProxyFactoryBean();

        //设置远程访问的服务的地址
        jax.setAddress("http://127.0.0.1:9999/Hello");

        //设置调用服务接口类型(会根据该接口生成接口的代理对象)
        jax.setServiceClass(UserServer.class);

        //生成服务接口的代理对象
        UserServer userService = jax.create(UserServer.class);

        //通过代理对象,远程调用服务方法
        String welcome = userService.welcome("Hello");
        //测试
        System.out.println(welcome);
    }
}

客户端输出

Hello,welcome!!

服务端输出

image

  • 观察SOAP协议的内容
    • boby的请求参数是Hello
    • 返回的参数是Hello,welcome!!
----------------------------
ID: 1
Address: http://127.0.0.1:9999/Hello
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
Headers: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], Content-Length=[185], content-type=[text/xml; charset=UTF-8], Host=[127.0.0.1:9999], Pragma=[no-cache], SOAPAction=[""], User-Agent=[Apache CXF 3.0.1]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:welcome xmlns:ns2="http://server.com/"><arg0>Hello</arg0></ns2:welcome></soap:Body></soap:Envelope>
--------------------------------------
2018-04-11 10:08:58,218 15680  [tp1424082571-16] INFO  .UserServerImplPort.UserServer  - Outbound Message
---------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:welcomeResponse xmlns:ns2="http://server.com/"><return>Hello,welcome!!</return></ns2:welcomeResponse></soap:Body></soap:Envelope>
--------------------------------------

猜你喜欢

转载自blog.csdn.net/kato_op/article/details/80338220