cxf小实例
来源:互联网 发布:人工智能论文话题英文 编辑:程序博客网 时间:2024/06/05 03:26
使用MyEclipse 创建Web Project ,添加 maven 支持。 生成项目结构如下:
2、POM.XML 文件内容如下 。(包含 cxf 与 spring 等的jar包)
- <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>cxf</groupId>
- <artifactId>cxf</artifactId>
- <packaging>war</packaging>
- <version>0.0.1-SNAPSHOT</version>
- <name>cxf Maven Webapp</name>
- <url>http://maven.apache.org</url>
- <properties>
- <cxf.version>2.4.3</cxf.version>
- <spring.version>2.5.6</spring.version>
- </properties>
- <dependencies>
- <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-transports-http</artifactId>
- <version>${cxf.version}</version>
- </dependency>
- <!-- Jetty is needed if you're are not using the CXFServlet -->
- <dependency>
- <groupId>org.apache.cxf</groupId>
- <artifactId>cxf-rt-transports-http-jetty</artifactId>
- <version>${cxf.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.cxf</groupId>
- <artifactId>cxf-tools-common</artifactId>
- <version>${cxf.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.cxf</groupId>
- <artifactId>cxf-tools-java2ws</artifactId>
- <version>${cxf.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.cxf</groupId>
- <artifactId>cxf-tools-validator</artifactId>
- <version>${cxf.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.cxf</groupId>
- <artifactId>cxf-tools-wsdlto-core</artifactId>
- <version>${cxf.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.cxf</groupId>
- <artifactId>cxf-tools-wsdlto-databinding-jaxb</artifactId>
- <version>${cxf.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${spring.version}</version>
- <type>jar</type>
- <scope>compile</scope>
- <exclusions>
- <exclusion>
- <artifactId>spring-web</artifactId>
- <groupId>org.springframework</groupId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring</artifactId>
- <version>${spring.version}</version>
- <type>jar</type>
- <scope>compile</scope>
- <exclusions>
- <exclusion>
- <artifactId>commons-logging</artifactId>
- <groupId>commons-logging</groupId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>org.apache.cxf</groupId>
- <artifactId>cxf-tools-wsdlto-frontend-jaxws</artifactId>
- <version>${cxf.version}</version>
- </dependency>
- </dependencies>
- <repositories>
- <repository>
- <id>apache-snapshots</id>
- <name>Apache SNAPSHOT Repository</name>
- <url>http://repository.apache.org/snapshots/</url>
- <snapshots>
- <enabled>true</enabled>
- </snapshots>
- </repository>
- <repository>
- <id>atlassian-m2-repository</id>
- <url>https://m2proxy.atlassian.com/repository/public</url>
- </repository>
- </repositories>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.cxf</groupId>
- <artifactId>cxf-java2ws-plugin</artifactId>
- <version>${cxf.version}</version>
- </plugin>
- </plugins>
- </build>
- </project>
3、创建服务接口类与实现类。(JDK 默认是1.6 u13 的,最好更换为 1.6 u 17以上)
- package demo;
- import javax.jws.WebParam;
- import javax.jws.WebService;
- import javax.jws.soap.SOAPBinding;
- import javax.jws.soap.SOAPBinding.Style;
- @WebService
- @SOAPBinding(style = Style.RPC)
- public interface IHelloWorld {
- public String sayHello(@WebParam(name = "name") String name);
- }
package demo;
- public class HelloWorldImpl implements IHelloWorld{
- public String sayHello(String name) {
- return name + " say: Hello World!";
- }
- }
4、Spring 配置文件
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://cxf.apache.org/jaxws
- http://cxf.apache.org/schemas/jaxws.xsd">
- <!-- 上面 1、 http://cxf.apache.org/schemas/jaxws.xsd
- 2、 http://cxf.apache.org/jaxws
- 3、http://cxf.apache.org/schemas/jaxws.xsd
- 为引入 CXF 的命名空间 -->
- <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"/>
- <!-- 上面3个 import 为导入 CXF 的 Bean 定义文件-->
- <!-- 定义具体实现的 Bean ,这个 Bean 的定义与 Spring 普通的 Bean 定义是一样的 -->
- <bean id="helloService" class="demo.HelloWorldImpl" />
- <!-- 通过 CXF 提供的 <jaxws:server> 标签,将定义的 Bean 暴露出去成为 Web Service 服务 -->
- <!-- serviceClass = 接口类 -->
- <!-- address = 调用时的URL -->
- <jaxws:server id="helloWebService"
- serviceClass="demo.IHelloWorld"
- address="/helloWorld">
- <!-- 要暴露的 bean 的引用,上面定义的bean id -->
- <jaxws:serviceBean>
- <ref bean="helloService"/>
- </jaxws:serviceBean>
- </jaxws:server>
- </beans>
5、WEB.XML 文件
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <display-name></display-name>
- <!-- 我们的示例是需要通过 Servlet 容器进行服务暴露,
- 因此需要配置相对应的 web.xml 文件,
- 首先是增加 Spring 的配置文件加载 Listene -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/Cxf-config.xml</param-value>
- </context-param>
- <!-- Spring ContextLoaderListener -->
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
- <!-- CXF Servlet 的定义,以及它的映射 -->
- <servlet>
- <servlet-name>CXFServlet</servlet-name>
- <servlet-class>
- org.apache.cxf.transport.servlet.CXFServlet
- </servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <!-- CXFServlet Mapping -->
- <servlet-mapping>
- <servlet-name>CXFServlet</servlet-name>
- <url-pattern>/*</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
6、发布并访问 http://localhost:9098/mycxf/helloWorld?wsdl 。
7、客户端Spring调用测试
8、客户端 Spring 文件
- <?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:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.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"/>
- <!-- 客户端 -->
- <jaxws:client id="helloServiceClient"
- serviceClass="demo.IHelloWorld"
- address="http://localhost:9098/mycxf/helloWorld"/>
- </beans>
9、客户端测试代码
- package client;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import demo.IHelloWorld;
- /**
- * 客户端调用,测试代码
- */
- public class HelloServiceClient {
- public static void main(String[] args) {
- // 加载客户端的配置定义
- ApplicationContext context = new ClassPathXmlApplicationContext("Cxf-client.xml");
- // 获取定义的 Web Service Bean
- IHelloWorld helloService = (IHelloWorld) context.getBean("helloServiceClient");
- String username = "Ailan";
- // 调用方法进行服务消费
- String result = helloService.sayHello(username);
- System.out.println("Result:" + result);
- }
- }
10、最终项目结构
11、附件中为完整代码。
0 0
- cxf小实例
- CXF 简单实例
- cxf 入门实例
- CXF 入门实例
- CXF 入门实例
- Apache CXF技术实例
- cxf 实例解读
- CXF动态调用实例
- cxf 实例解读
- CXF简单实例
- CXF框架入门实例
- CXF框架入门实例
- 简单的CXF实例
- WebService-CXF-实例
- CXF框架入门实例
- cxf 实例解读
- Cxf开发webservce实例
- cxf 实例解读
- 复制当前目录及其子目录下面的所有的txt文件,如果有文件名相同则重命名为bak01,bak02 ......
- 使用 Apache CXF 实现 Web Service 详解
- CXF整合spring
- Apache Cxf WebService整合Spring(1)
- Apache Cxf WebService整合Spring(2)
- cxf小实例
- Apache CXF 客户端调用 超时设置
- 用js来刷新当夜页面,保留参数
- Linux下python默认版本设置
- arch下ibus选择
- 大数据下Sql优化心得记录
- Node中的模块机制
- 异步I/O
- 黑马程序员_内部类总结