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://wenku.baidu.com/link?url=dxRkxXZwMTuD953KU9q7TYsN-ri-CfaqZPYdDyFh13Tp63ec3E-M4iFAnxRrt4Ar6qNbrdR2zNaw0fGShCb5i2WN6Ed2L9nKJCC8L0EsThC

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的更多相关文章

  1. WebService -- Java 实现之 CXF (初体验)

    1. 认识WebService 简而言之,她就是:一种跨编程语言以及操作系统的远程调用技术. 大家都可以根据定义好的规范和接口进行开发,尽管各自的使用的开发语言和操作系统有所不同,但是由于都遵循统一的 ...

  2. Apache CXF 103 CXF Basics - partial

    本Spike记录中内容,如无特别指出,均引用[1]. 0 引言 0.1 基本的Web服务术语 XML 业界的结构化交换信息表示的事实上的标准. XML namespace是在XML文档中提供唯一的命名 ...

  3. Apache CXF 102 CXF with REST

    前言 续上篇Apache CXF 101,摘抄部分REST概念性知识,以运行实例考察CXF对REST的支持. 目录 1 REST简介 2 工具 3 运行实例 内容 本Spike记录中内容,如无特别指出 ...

  4. 目录启动CXF启动报告LinkageError异常以及Java的endorsed机制

    本文纯属个人见解,是对前面学习的总结,如有描述不正确的地方还请高手指正~ Exception in thread "main" java.lang.LinkageError: JA ...

  5. apache CXF quickstart

    1下载 官网: cxf.apache.org 下载 CXF 的开发包: 解压上面的 zip 文件 : 2介绍 1什么是cxf Apache CXF™ is an open source service ...

  6. java调用CXF WebService接口的两种方式

    通过http://localhost:7002/card/services/HelloWorld?wsdl访问到xml如下,说明接口写对了. 2.静态调用 // 创建WebService客户端代理工厂 ...

  7. webService学习之路(三):springMVC集成CXF后调用已知的wsdl接口

    webService学习之路一:讲解了通过传统方式怎么发布及调用webservice webService学习之路二:讲解了SpringMVC和CXF的集成及快速发布webservice 本篇文章将讲 ...

  8. webService学习之路(二):springMVC集成CXF快速发布webService

    继上一篇webService入门之后,http://www.cnblogs.com/xiaochangwei/p/4969448.html ,现在我将我周六在家研究的结果公布出来 本次集成是基于之前已 ...

  9. CXF:根据werservice代码生成WSDL(转)

    原文:http://hongyegu.iteye.com/blog/619147,谢谢! import org.apache.cxf.tools.java2ws.JavaToWS; import ne ...

随机推荐

  1. 学习requests_html

    一.获取页面上的所有链接. from requests_html import HTMLSession session=HTMLSession() r=session.get('https://new ...

  2. jenkins安装部署全过程

    基本配置: 1.Linux安装配置jdk环境 1.1.上传到 Linux 服务器:例如: 上传至: cd /usr/local 1.2.解压: rpm -ivh jdk-8u111-linux-x64 ...

  3. IDEA的校园邮箱激活方式

    链接: https://blog.csdn.net/m0_37286282/article/details/78279060

  4. CSS: hover选择器的使用

    用法1:这个表示的是:当鼠标悬浮在a这个样式上的时候,a的背景颜色设置为黄色 a:hover        {             background-color:yellow;        ...

  5. UVA1388 Graveyard

    思路 就是对于每个点,找出离他最近的目标点的距离 我使用了上取整和下取整实现,蓝书上的实现方法是坐标系缩放,每个点的目标位置就是它四舍五入的结果 具体证明见蓝书 代码 #include <cst ...

  6. [CodeForces 471A] MUH and Sticks

    题目链接:http://codeforces.com/problemset/problem/471/A 题目数据规模1 - 9,可以用一个数组进行计数,减掉出现四次的数,看看还有几个是非零数,有一个就 ...

  7. Kubernetes 路由问题&网络问题

    error 信息: kubectl 获取node的host地址 kubectl get pods -n $namespace -o wide 或者在Kubernetes的service中进行查看 ku ...

  8. CentOS7.3上如何安装Apache/2.4.34

    1)卸载系统自带的httpd Centos可能自带了httpd,但是版本可能会较低,执行下面的命令检测是否已经安装了httpd rpm -qa | grep httpd 如果检测已经安装了二进制的ht ...

  9. 02.Vue基本代码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. excel加密文件破解代码

    1. AIT+F11  2. 代码   3. F5 Public Sub AllInternalPasswords()' Breaks worksheet and workbook structure ...