CXF develop Webserice Tuturial
1. 修改pom.xml 在Maven中引入CXF 依赖包
1.1 引入CXF依赖包 ,配置Tomcat插件及其它
<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.company</groupId>
<artifactId>testProject</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Test Maven Webapp</name>
<url>http://maven.apache.org</url> <properties>
<spring.version>3.1.1.RELEASE</spring.version>
<cxf.version>2.7.12</cxf.version>
</properties> <build>
<finalName>testProject</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument></compilerArgument>
<properties>
<debug>true</debug>
<compilerVersion>1.6</compilerVersion>
<maven.compiler.forceJavacCompilerUse>true
</maven.compiler.forceJavacCompilerUse>
</properties>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.0</version> <configuration>
<path>/${project.build.finalName}</path>
<port>${tomcat.http.port}</port>
<uriEncoding>UTF-8</uriEncoding>
<url>http://localhost:${tomcat.http.port}/manager/text</url>
<finalName>teab</finalName>
<server>tomcat6</server>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<path>/${project.build.finalName}</path>
<port>${tomcat.http.port}</port>
<uriEncoding>UTF-8</uriEncoding>
<url>http://localhost:${tomcat.http.port}/manager/text</url>
<finalName>teab</finalName>
<server>tomcat7</server>
</configuration>
</plugin>
</plugins>
</build> <dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-common</artifactId>
<version>2.5.9</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-core</artifactId>
<version>${cxf.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
<type>jar</type>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies> </project>
注意配置文件中的exclusions , 这些为了避免引入不同版本的 javax..servlet.servletcontext class 文件,导致类加载出现冲突。
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/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置Spring容器加载配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <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>
</web-app>
3. 创建webservice接口
package com.sysware.demo.app.service.inf; import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService; import com.sysware.demo.app.model.RetInfo; @WebService
public interface IGetInfoService
{
@WebMethod(operationName = "add")
@WebResult(name = "result")
public int add(@WebParam(name = "num1") int num1,
@WebParam(name = "num2") int num2); @WebMethod(operationName = "getRetInfo")
@WebResult(name = "result")
public RetInfo getRetInfo(@WebParam(name = "name") String name, @WebParam(name = "age") int age);
}
4. 创建webservice实现类
package com.sysware.demo.app.service.impl; import javax.jws.WebService; import com.sysware.demo.app.model.RetInfo;
import com.sysware.demo.app.service.inf.IGetInfoService; @WebService(endpointInterface = "com.sysware.demo.app.service.inf.IGetInfoService")
public class GetInfoServiceImpl implements IGetInfoService
{ @Override
public int add(int num1, int num2)
{
return num1 + num2;
} @Override
public RetInfo getRetInfo(String name, int age)
{
RetInfo retInfo = new RetInfo();
retInfo.setAge(age);
retInfo.setName(name);
return retInfo;
} }
5. 创建返回对象
package com.sysware.demo.app.model; public class RetInfo
{
private String name;
private int age;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
}
6. 创建bean文件applicationContext.xml在WEB-INF目录下
<?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-servlet.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-xml.xml"/>
<bean id="getInfoServiceImpl" class="com.sysware.demo.app.service.impl.GetInfoServiceImpl"></bean>
<jaxws:endpoint id="getInfoService" implementor="#getInfoServiceImpl" address="/getInfoService"></jaxws:endpoint>
</beans>
7. 运行Application
7.1 打包应用程序
mvn clean package
7.2 运行程序
#run the app in tomcat6 container
mvn tomcat:run #or run in tomcat7 container
mvn tomcat7:run
或者
#run in jetty container
mvn jetty:run-war
8. jquery access the webservice
index.html
<html>
<head>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
<script type="text/javascript" src="js/teab.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(btnAjaxPost);
});
</script>
</head> <body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button type="button">call web service</button>
</body>
</html>
function btnAjaxPost(event) {
$.ajax({
type: "GET",
contentType:"application/json",
url:"services/productServices/product/1245",
dataType:'json',
success: function(json) {
alert('sucess:'+JSON.stringify(json));
},
error: function(x, e) {
alert('error:'+x.responseText);
},
complete: function(x) {
//alert('complete:'+x.responseText);
}
});
}
8. 验证Webservice 是否启动成功
如果是在Jetty 启动访问下面链接
http://localhost:8080/ws/getInfoService?wsdl
如果是Tomcat容器中启动则访问如下链接
http://localhost:8080/your-application-name/ws/getInfoService?wsdl
reference documents
http://www.benmccann.com/blog/web-services-tutorial-with-apache-cxf/
北风网—— CXF框架快速起步_1
http://v.youku.com/v_show/id_XNDk3Nzg0NzY0.html?from=y1.2-1-87.3.2-1.1-1-1-1
北风网—— CXF框架快速起步_2
http://v.youku.com/v_show/id_XNDk3Nzg2OTYw.html
maven+spring+cxf编写web service
http://blog.csdn.net/johnnywww/article/details/7991753
CXF 学习一(创建Server和Client)
http://blog.csdn.net/tangyajun_168/article/details/6709186
Building RESTful Web services with Apache CXF and Spring
http://www.27programs.com/2013/11/09/building-restful-web-services-apache-cxf-spring/
CXF develop Webserice Tuturial的更多相关文章
- WebService -- Java 实现之 CXF (初体验)
1. 认识WebService 简而言之,她就是:一种跨编程语言以及操作系统的远程调用技术. 大家都可以根据定义好的规范和接口进行开发,尽管各自的使用的开发语言和操作系统有所不同,但是由于都遵循统一的 ...
- Apache CXF 103 CXF Basics - partial
本Spike记录中内容,如无特别指出,均引用[1]. 0 引言 0.1 基本的Web服务术语 XML 业界的结构化交换信息表示的事实上的标准. XML namespace是在XML文档中提供唯一的命名 ...
- Apache CXF 102 CXF with REST
前言 续上篇Apache CXF 101,摘抄部分REST概念性知识,以运行实例考察CXF对REST的支持. 目录 1 REST简介 2 工具 3 运行实例 内容 本Spike记录中内容,如无特别指出 ...
- 目录启动CXF启动报告LinkageError异常以及Java的endorsed机制
本文纯属个人见解,是对前面学习的总结,如有描述不正确的地方还请高手指正~ Exception in thread "main" java.lang.LinkageError: JA ...
- apache CXF quickstart
1下载 官网: cxf.apache.org 下载 CXF 的开发包: 解压上面的 zip 文件 : 2介绍 1什么是cxf Apache CXF™ is an open source service ...
- java调用CXF WebService接口的两种方式
通过http://localhost:7002/card/services/HelloWorld?wsdl访问到xml如下,说明接口写对了. 2.静态调用 // 创建WebService客户端代理工厂 ...
- webService学习之路(三):springMVC集成CXF后调用已知的wsdl接口
webService学习之路一:讲解了通过传统方式怎么发布及调用webservice webService学习之路二:讲解了SpringMVC和CXF的集成及快速发布webservice 本篇文章将讲 ...
- webService学习之路(二):springMVC集成CXF快速发布webService
继上一篇webService入门之后,http://www.cnblogs.com/xiaochangwei/p/4969448.html ,现在我将我周六在家研究的结果公布出来 本次集成是基于之前已 ...
- CXF:根据werservice代码生成WSDL(转)
原文:http://hongyegu.iteye.com/blog/619147,谢谢! import org.apache.cxf.tools.java2ws.JavaToWS; import ne ...
随机推荐
- QuerySet API
模型objects:这个对象是 django.db.model.manager.Manger 的对象,这个类是一个空壳类,它上面的所有方法都是从 QuerySet 这个类中拷贝过来的. >> ...
- python版 mapreduce 矩阵相乘
参考张老师的mapreduce 矩阵相乘. 转载请注明:来自chybot的学习笔记http://i.cnblogs.com/EditPosts.aspx?postid=4541939 下面是我用pyt ...
- vue--实例化对象
根目录下的文件,这些是创建vue项目时生成的配置文件 node_modules=> 里面的文件是项目开发过种中的各种依赖,我们暂且不用去深入了解: public=> 主要放的是一些公用的文 ...
- _mount_allowed
该表配置可以坐骑的使用区域,可能需要修改spell.dbc,允许在室内等特殊区域使用坐骑技能
- 使用metrics统计接口tps
metrics的简单介绍 metrics是一种性能指标工具,有很多开源工具使用之来来作为其性能监控,如Hadoop,Kafka,Spark,JStorm等. metrics使用最主要有三个东西: Me ...
- php 中swoole安装
1.下载扩展包: 网址:http://pecl.php.net/ 中(http://pecl.php.net/get/swoole-4.3.1.tgz) 2.解压安装包. 3.进入解压好的安装包. 4 ...
- 树状DP HDU1520 Anniversary party
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题意:职员之间有上下级关系,每个职员有自己的happy值,越高在派对上就越能炒热气氛.但是必须是 ...
- MVC 简介
是AOP (Aspect-Oriented Programming.面向侧 面的程序设计或面向方面的程序设计,是对面向对象程序设计的改进和扩展)内的概念 当 一 件事被细分为各个单元后,各个单元的复 ...
- bzoj3884 上帝的集合
根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α α ”.“α α 被定义为“元”构成的集合.容 ...
- day18_python_1124
01 鸡汤 荷花定律 山竹定律 金蝉定律 荷花定律: 明天比今天要生长多一倍,池塘三十天生长满,第29天是一半. 山竹定律: 竹子在生长时,前三年只出地表3厘米,从第四年开始,以每天30cm的速度开始 ...