spring整合cxf 使用jax-ws規範實現webservice功能

一.新建服務端

新建項目:spring-jax-ws-server

1.pom文件的編寫:

 

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>web-service-demo</artifactId>
        <groupId>com.meboth.bonc.ljf</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.meboth.bonc.ljf.spring.jax</groupId>
    <artifactId>spring-jax-ws-server</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>spring-jax-ws-server Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!-- 要進行jaxws 服務開發 -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.2</version>
        </dependency>
        <!-- 內置jetty web服務器 -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.1.2</version>
        </dependency>
        <!-- 日誌實現 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
        </dependency>
        <!--spring 需要的jar包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
      <!-- 解決jar包衝突-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>


    </dependencies>

    <build>
        <finalName>spring-jax-ws-server</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <!-- maven的jdk編譯插件 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                        <encoding>UTF-8</encoding>
                        <showWarnings>true</showWarnings>
                    </configuration>
                </plugin>
                <!-- 運行tomcat7方法:tomcat7:run -->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <!-- 指定端口 -->
                        <port>8080</port>
                        <!-- 請求路徑 -->
                        <path>/</path>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

2.接口實現類

package com.ljf.spring.jax.ws.server.service;

import javax.jws.WebService;

@WebService
public interface BeiJingUserService {
    //問好
    public String sayHelloToBeijingName(String name);
}





package com.ljf.spring.jax.ws.server.serviceImpl;

import com.ljf.spring.jax.ws.server.service.BeiJingUserService;

public class BeiJingUserServiceImpl implements BeiJingUserService {
    @Override
    public String sayHelloToBeijingName(String name) {
        return name+":nihao ,today is 端午節!!!!";
    }
}

3.resources文件下的applicationContext資源文件

<?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:cxf="http://cxf.apache.org/core"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://cxf.apache.org/core
        http://cxf.apache.org/schemas/core.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd
        http://cxf.apache.org/jaxrs
        http://cxf.apache.org/schemas/jaxrs.xsd
">

    <!--
        Spring整合ApacheCXF,發佈jaxws服務:
        1. 服務地址
        2. 服務bean        完整服務地址:
        http://localhost:8080/ws/userService
     -->
    <jaxws:server address="/beijinguser">  <!--可以任意起名 -->
        <jaxws:serviceBean>
            <bean class="com.ljf.spring.jax.ws.server.serviceImpl.BeiJingUserServiceImpl"></bean>
        </jaxws:serviceBean>
    </jaxws:server>
</beans>

4.web.xml文件 

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- webservice服務端,發佈服務需要配置CXFServlet -->
  <!-- 這裏配置的servlet路徑,最爲最終服務路徑的一部分: -->
  <!-- 服務訪問路徑:http://localhost:8080/web.xml配置路徑/spring配置的路徑 -->
  <servlet>
    <servlet-name>cxfServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>cxfServlet</servlet-name>
    <url-pattern>/ws/*</url-pattern>
  </servlet-mapping>
</web-app>

5.index.jsp頁面

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

 

6.啓動tomcat服務:

二.新建客戶端

新建項目:spring-jax-ws-client

1.pom文件:

<?xml version="1.0" encoding="UTF-8"?>

<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">
    <parent>
        <artifactId>web-service-demo</artifactId>
        <groupId>com.meboth.bonc.ljf</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.meboth.bonc.ljf.spring.jax</groupId>
    <artifactId>spring-jax-ws-client</artifactId>
    <packaging>war</packaging>

    <name>spring-jax-ws-client Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!-- 要進行jaxws 服務開發 -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.2</version>
        </dependency>
        <!-- 內置jetty web服務器 -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.1.2</version>
        </dependency>
        <!-- 日誌實現 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
        </dependency>
        <!--spring 需要的jar包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!-- 解決jar包衝突-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>spring-jax-ws-client</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <!-- maven的jdk編譯插件 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                        <encoding>UTF-8</encoding>
                        <showWarnings>true</showWarnings>
                    </configuration>
                </plugin>
                <!-- 運行tomcat7方法:tomcat7:run -->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <!-- 指定端口 -->
                        <port>8080</port>
                        <!-- 請求路徑 -->
                        <path>/</path>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

2.接口

package com.ljf.spring.jax.ws.client.service;

import javax.jws.WebService;

@WebService(targetNamespace = "http://service.server.ws.jax.spring.ljf.com/")
public interface BeiJingClientUserService {
    //注意要和服務端的方法名保持一致,不一致的話,報錯
    public String sayHelloToBeijingName(String name);
}

 

3.資源配置類:applicationContext的內容

<?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:cxf="http://cxf.apache.org/core"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://cxf.apache.org/core
        http://cxf.apache.org/schemas/core.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd
        http://cxf.apache.org/jaxrs
        http://cxf.apache.org/schemas/jaxrs.xsd
">
    <!--
           Spring整合ApacheCXF,客戶端配置
           關鍵點:
               通過Spring整合ApacheCXF,創建客戶端的代理對象,遠程訪問服務端。
           jaxws:client
               id  應用中注入的接口的代理對象的名稱
               address 服務端訪問地址
               serviceClass  指定接口路徑,會根據該類型生成代理對象
       -->
    <jaxws:client
            id="bcuserClient"
            address="http://localhost:8080/ws/beijinguser"
            serviceClass="com.ljf.spring.jax.ws.client.service.BeiJingClientUserService"></jaxws:client>
</beans>

 

4.啓動類:

package com.ljf.spring.jax.ws.client;

import com.ljf.spring.jax.ws.client.service.BeiJingClientUserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import javax.annotation.Resource;


public class SpringJaxClient {
    // 注入遠程訪問服務端的接口的代理對象。

    public static void main(String args[]){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext.xml" });
        BeiJingClientUserService userService = (BeiJingClientUserService) context.getBean("bcuserClient");
        // org.apache.cxf.jaxws.JaxWsClientProxy@2826f61
        System.out.println(userService);
        // class com.sun.proxy.$Proxy45
        System.out.println(userService.getClass());
        // 遠程調用服務接口
        String content = userService.sayHelloToBeijingName("球球");
        System.out.println(content);




    }
}

注意這裏的web.xml和index.jsp頁面,可以忽略。

5.調用(確保服務啓動,且是正常的)

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章