首页 > 其他分享 >Spring CXF实例

Spring CXF实例

时间:2023-06-04 15:32:00浏览次数:39  
标签:CXF spring version springframework 实例 cxf Spring org public


[b][color=red]参考:[/color][/b]CXF Spring整合 ——又一个helloword! [url]http://pphqq.iteye.com/blog/1447800[/url]

一个依赖问题:

A: [color=red]如果希望以一种一致的方式实现webservice,特别是有跨语言的需求时,应该使用[b]Axis2[/b][/color]
B: [color=red]如果需要在现有的java程序(包括web应用)中增加webservice支持,应该使用[b]CXF[/b][/color]

注解:
标注 说明
WebService 将 Java 类标记为实现 Web Service,或者将 Java 接口标记为定义 Web Service 接口
WebMethod 定制Web Service方法
WebParam 定制Web Service方法的参数
WebResult 定制Web Service方法的返回值
SOAPBinding 指定WebService的SOAP映射样式


pom.xml

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>SpringCXF</groupId>
    <artifactId>SpringCXF</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>3.2.8.RELEASE</spring.version>
        <cxf.version>2.4.1</cxf.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- CXF Dependencies -->
        <dependency>
            <groupId>jta</groupId>
            <artifactId>jta</artifactId>
            <version>1.0.1b</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-core</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-simple</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-databinding-aegis</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-local</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>${cxf.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.geronimo.specs</groupId>
                    <artifactId>geronimo-servlet_3.0_spec</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-jms</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-management</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-common-utilities</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <!-- End of CXF Dependencies -->

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <url>http://localhost:8080/manager/text</url>
                    <username>admin</username>
                    <password>admin</password>
                    <path>/SpringCXF</path>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>




web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0">
    <display-name>Spring3MVC</display-name>
    <!--        配置SpringMVC的applicationContext.xml文件位置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!--       配置SpringMVC -->
    <servlet>
        <servlet-name>spring-mvc-servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-mvc-servlet</servlet-name>
        <url-pattern>/web/*</url-pattern>
    </servlet-mapping>
    <!--       配置SpringWS -->
    <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>/ws/*</url-pattern>
    </servlet-mapping>

    <!--<listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>




spring-mvc-servlet.xml


<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" />
    </context:component-scan>

    <mvc:annotation-driven/>

    <mvc:resources location="/resources/" mapping="/resources/**"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>




applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans"
       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:p="http://www.springframework.org/schema/p"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://cxf.apache.org/jaxws
    http://cxf.apache.org/schemas/jaxws.xsd"
       default-autowire="byName" default-lazy-init="true">

    <context:component-scan base-package="com">
        <context:exclude-filter expression="org.springframework.stereotype.Controller"  type="annotation" />
    </context:component-scan>

    <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" />

    <!--        定义一个发布点,和发布点的地址 -->
    <jaxws:endpoint id="helloWorld" implementor="com.service.impl.HelloWorldImpl" address="/HelloWorld" />

</beans>




User.java


package com.module;

import java.io.Serializable;

/**
 * Created by Administrator on 14-5-21.
 */
public class User implements Serializable {


    private static final long serialVersionUID = 2035807023987192387L;
    private String userName;
    private String userCode;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserCode() {
        return userCode;
    }

    public void setUserCode(String userCode) {
        this.userCode = userCode;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof User)) return false;

        User user = (User) o;

        if (userCode != null ? !userCode.equals(user.userCode) : user.userCode != null) return false;
        if (userName != null ? !userName.equals(user.userName) : user.userName != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = userName != null ? userName.hashCode() : 0;
        result = 31 * result + (userCode != null ? userCode.hashCode() : 0);
        return result;
    }


    @Override
    public String toString() {
        return "User{" +
                "userName='" + userName + '\'' +
                ", userCode='" + userCode + '\'' +
                '}';
    }
}




下面开始写服务器端代码,首先定制服务器端的接口,代码如下:


IHelloWorld.java


package com.service;

import javax.jws.WebService;

/**
 * Created by Administrator on 14-5-21.
 * 定制服务器端的接口
 */
@WebService
public interface IHelloWorld {
    public String sayHello(String name);

    public User getUser(String name);
}




下面编写WebService的实现类,服务器端实现代码如下:


注意的是和Spring集成,这里一定要完成接口实现,如果没有接口的话会有错误的。


HelloWorldImpl.java


package com.service.impl;

import com.service.IHelloWorld;

/**
 * Created by Administrator on 14-5-21.
 * WebService的实现类,服务器端实现代码
 * 注意的是和Spring集成,这里一定要完成接口实现,如果没有接口的话会有错误的。
 */
public class HelloWorldImpl implements IHelloWorld {


    public String sayHello(String name) {
        System.out.println("sayHello is called by " + name);
        return "服务器返回的信息[" + name+"]";
    }


    @Override
    public User getUser(String name) {
        User user = new User();
        user.setUserCode("Pandy");
        user.setUserName(name);
        return user;
    }
}




下面要在applicationContext-server.xml文件中添加如下配置: applicationContext-client.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.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-extension-soap.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

    <!--
        定义一个客户端的类,用来连接WebService的信息
             这个和服务端开放的借口是实现同一个类的。
    -->
    <jaxws:client id="helloWorldClient" address="http://localhost:8080/SpringCXF/ws/HelloWorld" serviceClass="com.service.IHelloWorld"/>
</beans>


下面启动tomcat服务器后,在WebBrowser中请求:


[url]http://localhost:8080/SpringCXF/ws/HelloWorld?wsdl[/url]


----------------------


<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions name="HelloWorldImplService" targetNamespace="http://impl.service.com/" xmlns:ns1="http://service.com/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.service.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <wsdl:import location="http://localhost:8080/SpringCXF/ws/HelloWorld?wsdl=IHelloWorld.wsdl" namespace="http://service.com/">
    </wsdl:import>
  <wsdl:binding name="HelloWorldImplServiceSoapBinding" type="ns1:IHelloWorld">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="getUser">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="getUser">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="getUserResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="sayHello">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="sayHello">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="sayHelloResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="HelloWorldImplService">
    <wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort">
      <soap:address location="http://localhost:8080/SpringCXF/ws/HelloWorld"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>


如果你能看到wsdl的xml文件的内容,就说明你成功了,注意的是上面地址的HelloWorld就是上面xml配置中的address的名称,是一一对应的。




下面编写客户端请求的代码,代码如下:


Client.java


package com.client;

/**
 * Created by Administrator on 14-5-21.
 * 客户端调用服务端,许多东西都被Spring和CXF自己处理了,只实现调用和输出就好。
 *
 */
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.service.IHelloWorld;

public class Client {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml");
        IHelloWorld helloWorld = (IHelloWorld) context.getBean("helloWorldClient");
        System.out.println(helloWorld.sayHello("客户端传进去的信息"));
        System.out.println(helloWorld.getUser("Panyongzheng").toString());
    }

}




先运行服务器


在以应用程序的方式运行客户端,得到输出:


......


org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass


信息: Creating Service {http://service.com/}IHelloWorldService from class com.service.IHelloWorld


[color=darkblue]服务器返回的信息[客户端传进去的信息][/color]


标签:CXF,spring,version,springframework,实例,cxf,Spring,org,public
From: https://blog.51cto.com/u_3871599/6410805

相关文章

  • Spring和MyBatis整合
    框架整合时三层架构的分工  进行SSM框架整合时,两个框架的分工如下所示。MyBatis负责与数据库进行交互。Spring负责事务管理,Spring可以管理持久层的Mapper对象及业务层的Service对象。由于Mapper对象和Service对象都在Spring容器中,所以可以在业务逻辑层通过Service对象调用......
  • CXF入门教程(2) -- 第一个客户端
    文中对应的代码已经上传,与教程(1)中的service相对应。为调试方便,将service和client都放在了同一个工程中,不过是在不同的包中;本文对应的两个基本客户端在com.neareast.test.cxf.client.consumer包中,服务端ServiceTest类在com.neareast.test.cxf.server.service包中。代码地址如下:最常......
  • Spring3.2 + cxf1.7.3整合
    参考:[url]http://tsinglongwu.iteye.com/blog/832704[/url][color=red][b]测试调试工具介绍[/b][/color][b]1.SoapUI1.6[/b][url]http://webservices.ctocio.com.cn/tips/263/7817763.shtml[/url][b]2.TestMaker[/b][url]http://webservices.ctocio.com......
  • SpringMVC3.2.x + Hibernate4.2.x + ecache + Spring Security 3.0.5
    这只是部分代码,一些代码可以参考:[url]http://panyongzheng.iteye.com/blog/1871418[/url]SpringSecurity3.1最新配置实例[url它自带的附件也上传。SpringSecurity3十五日研究[url]http://www.blogjava.net/SpartaYew/archive/2013/09/23/350630.html[/......
  • Spring整合mybatis使用xml配置事务
    自己准备开始教授Java相关的技术,Spring框架是必须让学生学习的框架之一。里面有一个事务的配置以前刚学习Spring框架的时候有接触过,不过已经过了很多年,很多东西都已经忘记。现在再来回忆一下如何使用Spring框架类配置事务。使用到的maven坐标如下:<dependencies>    <......
  • 从日志记一次Spring事务完整流程
    spring事务一次完整流程,创建》确认获取连接》完成》提交》释放链接DataSourceTransactionManager//Step1.进入业务方法前,依据事物切面创建事务对象2019-07-0622:34:24,819[main]DEBUGo.s.j.d.DataSourceTransactionManager-Creatingnewtransactionwithname[com......
  • Spring 3.0.5+MyBatis3.0.4整合非完全例子
    基于注解的mybatis和spring整合:[url]http://huangmin001.iteye.com/blog/1185806[/url][color=red]这个文章说的很详细,很值得一看[/color].Maven+SpringMVC+Mybatis【绝非原创,单纯整理】【四】:[url]http://playgod1984.iteye.com/blog/984113[/ur......
  • spring jdbcTemplate使用
    参考:springjdbcTemplate使用[url]http://log-cd.iteye.com/blog/215059[/url]SpringJdbcTemplate与事务管理学习[url]http://www.iteye.com/topic/480432[/url]SimpleJdbcTemplate在spring3.1已经过时了,我就改为使用jdbcTemplate和namedParameterJdbcOperations写sql查询......
  • 数据库管理工具远程连接MySQL实例服务失败Host ... is not allowed to connect to thi
    MySQL社区版数据库8.0版本添加账号、分配权限、删除账号MySQL安全连接失败问题排查......
  • SpringSecurity使用JWT
    SpringSecurity的UsernamePasswordAuthenticationFilter用于处理认证。要整合JWT,只需在认证成功后生成TOKEN并通过响应头写回客户端。在新增一个过滤器用于校验TOKEN。新建SpringBoot项目,添加依赖:<dependency><groupId>org.springframework.boot</groupId>......