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 ...
随机推荐
- SQL Developer连接Oracle出现“IO 错误:Undefined Error”
1.环境 Win 10系统 Oracle 11 g R 2 JDK 1.8.0_152 SQL Developer-17.2.0 2.安装完成后,运行SQL developer,选择JDK路径,连接出 ...
- trueStudio中使用printf函数
1.通过printf输出浮点数需要如下设置: 在工程属性下找到C/C++ build->Settings->Tool Settings->C Linker->Miscellan ...
- SpringBoot 配置阿里巴巴Druid连接池
在Spring Boot下默认提供了若干种可用的连接池(dbcp,dbcp2, tomcat, hikari),当然并不支持Druid,Druid来自于阿里系的一个开源连接池,它提供了非常优秀的监控功 ...
- 【转】 ISP概述、工作原理及架构
1.概述 ISP全称Image Signal Processing,即图像信号处理.主要用来对前端图像传感器输出信号处理的单元,以匹配不同厂商的图象传感器. ISP 通过一系列数字图像处理算法完成对数 ...
- ActiveRecord Nested Atrributes 关联记录,对嵌套属性进行CURD
设置了Nested attributes后,你可以通过父记录来更新/新建/删除关联记录. 使用: #accepts_nested_attributes_for class method. 例如: cl ...
- FileProvider 添加二级目录
我们在做Android N升级适配的时候 传统的Intent调用文件的方式会被认为不安全的 然后系统需要让我们使用更加安全的FileProvider的方法去构建intent请求 如 拍照,安装新的ap ...
- liunx文件操作 文件压缩
文件备份和压缩命令 在Linux中,常用的文件压缩工具有gzip,bzip2,zip. 'bzip2'是最理想的压缩工具,它提供了最大限度的压缩. 'zip'兼容好,windows也支持. bzip2 ...
- SpringBoot使用CORS解决跨域请求问题
什么是跨域? 同源策略是浏览器的一个安全功能,不同源的客户端脚本在没有明确授权的情况下,不能读写对方资源. 同源策略是浏览器安全的基石. 如果一个请求地址里面的协议.域名和端口号都相同,就属于同源. ...
- js生成指定范围的随机数
<!doctype html> <html lang="en"> <head> <meta http-equiv="Conten ...
- mac 常用终端命令
mkdir 文件夹夹名称 创建一个文件夹 cd 文件夹名称 进入该文件夹 git init 在该文件夹下创建一个git仓库 touch 文件名称 在该文件夹下创建一个文件 echo '内容' &g ...