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 ...
随机推荐
- Javascript中DataGrid表格纵线添加数据
接之前写的一篇博客http://www.cnblogs.com/Liu30/p/7229641.html,生成一个6*24的表格之后,添加数据 表格数据一般都是按行添加,我所做的这个表格是想添加一天2 ...
- TypeScript作业
题目: 了解神话故事盘古开天辟地或者女娲开世造物,通过typescript程序模拟出天地的变化过程或者万物的衍生过程 参考博客园大神: https://www.cnblogs.com/tansm/p/ ...
- Kotlin 基本数据类型
Kotlin 的基本数值类型包括 Byte.Short.Int.Long.Float.Double 等.不同于Java的是,字符不属于数值类型,是一个独立的数据类型. 类型 位宽度 Double 64 ...
- git项目远程地址修改后本地如何处理
今天运维人员为了方便管理,将远程的项目地址给迁移了, 原来是 git@git.lalala.com:yuanlaide/happy.git 变成了 git@git.lalala.com:houlaid ...
- loj#2059. 「TJOI / HEOI2016」字符串 sam+线段树合并+倍增
题意:给你一个子串,m次询问,每次给你abcd,问你子串sa-b的所有子串和子串sc-d的最长公共前缀是多长 题解:首先要求两个子串的最长公共前缀就是把反过来插入变成最长公共后缀,两个节点在paren ...
- javascript高级程序设计第3版——第一章概括
最近发现Xmind思维导图是个好东西,刚好开始看书,被用来归纳最好不过了
- python--接口开发
一.接口开发需要用到flask类1.首先安装flask类:cmd--pip install flask2.导入flask类:import flask3.以下是用一个例子来说明: import flas ...
- [la P5031&hdu P3726] Graph and Queries
[la P5031&hdu P3726] Graph and Queries Time Limit: 10000/5000 MS (Java/Others) Memory Limit: ...
- [poj P1475] Pushing Boxes
[poj P1475] Pushing Boxes Time Limit: 2000MS Memory Limit: 131072K Special Judge Description Ima ...
- 线程池threadPools
1.线程池是用来存储线程的容器 2.Executors.newFixedThreadPool(int n);创建线程池,并且设置线程池的容量为n 3.submit开启线程 4.会返回一个对象futur ...