• cxf+spring+soap简单接口开发


    最近学了cxf框架开发webservice,简单搭了个接口,方便后续翻阅,本人才疏学浅,若有不足,请多多谅解!

    一、服务端:

    1.所用到的jar包:
      

    maven的pom.xml配置:

    <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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.CXFWebService</groupId>
      <artifactId>cxf-demo</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>cxf-demo Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <!-- 添加 Spring dependency -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-tx</artifactId>
             <version>4.1.7.RELEASE</version>
            </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
         
        <!-- 添加CXF dependency  -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-core</artifactId>
            <version>3.1.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.5</version>
        </dependency>
      </dependencies>
      <build>
        <finalName>cxf-demo</finalName>
      </build>
    </project>

    2.配置web.xml:

    <?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"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">
        <display-name>cxf-demo</display-name>
        
        <!-- Spring config-->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:applicationContext*.xml</param-value>
        </context-param>
          
        <!-- Spring listener -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!-- Add on a servlet to handle web service request -->
        <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>/webservice/*</url-pattern> 
        </servlet-mapping>
    </web-app>

    3.配置applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:jaxws="http://cxf.apache.org/jaxws"
        xsi:schemaLocation="
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd  
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/cache 
        http://www.springframework.org/schema/cache/spring-cache-3.1.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-servlet.xml" />
        <bean id="helloWordServiceImpl" class="com.webservice.HelloWordServiceImpl"></bean>
        <jaxws:endpoint id="helloWordWebService" implementor="#helloWordServiceImpl" address="/helloWordWebService"></jaxws:endpoint>
        
    </beans>

    4.建一个service:HelloWordService

    package com.webservice;
    
    import javax.jws.WebService;
    
    @WebService
    public interface HelloWordService {
        
        public String sayHello(String name);
    }

    以及他的实现类:

    package com.webservice;
    
    import javax.jws.WebService;
    
    import org.springframework.stereotype.Component;
    
    @Component("helloWorld")
    @WebService
    public class HelloWordServiceImpl implements HelloWordService {
    
        public String sayHello(String name) {
            return "Hello Word,"+name;
        }
    
    }

    服务端就已经开发完毕,部署tomcat,访问:http://localhost:8080/cxf-demo/webService/helloWordService?wsdl,地址根据自己配置的为准,如果可以访问,就说明成功。

    二、客户端

    建立客户端

    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
            org.apache.cxf.endpoint.Client client = dcf.createClient("xxxx?wsdl");
            
            Object[] objects;
            try {
                objects = client.invoke("方法名", "请求内容");
                System.out.println(objects[0].toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
  • 相关阅读:
    three.js详解
    Javascript的原型
    Transform? Transition? Animation?
    Backbone源码分析Backbone架构+流程图
    对象是引用的注意原型中的属性改变
    MySQL+ JSP+Tomcat開發指引
    MySQL應用分析
    SQL 日期
    MySQL安裝
    MSE错误应对分享
  • 原文地址:https://www.cnblogs.com/ctsx/p/9233770.html
Copyright © 2020-2023  润新知