鹏叔(https://www.pengtech.net)

导航

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>
View Code

 

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);
        }
    });
}
View Code

 

 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/

 

posted on 2014-10-05 21:40  eagle.supper  阅读(278)  评论(0编辑  收藏  举报