这个例子需要建三个Maven项目,其中一个为父项目,另外两个为子项目

首先,建立父项目testParent,选择quickstart:

输入项目名称和模块名称,然后创建:

然后建立子项目testInterface:

再建立子项目projectImpl,过程跟上面以上,只是类型要选webapp(到时候启动工程或者打包都是以这个项目为主的):

建好之后,在testParent的pom.xml里面写入下面内容,主要是加入了cxf,junit,logback,springframework的依赖,在父项目加入依赖,子项目就不需要再加入依赖了:

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>testParent</groupId>
<artifactId>testParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging> <name>testParent</name>
<url>http://maven.apache.org</url> <build>
<finalName>testParent</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.version>3.2.8.RELEASE</org.springframework.version>
<cxf.version>2.7.7</cxf.version>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-api</artifactId>
<version>${cxf.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-security-cors</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-policy</artifactId>
<version>${cxf.version}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-core</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency> <dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>4.7</version>
<exclusions>
<exclusion>
<artifactId>logback-classic</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
<exclusion>
<artifactId>logback-core</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-databind</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion> </exclusions>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
</dependencies>
<modules>
<module>testInterface</module>
<module>projectImpl</module>
</modules>
</project>

然后在testInterface的src/main/java下面新建类

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; @Path("/a")
public interface TestIn1 { @GET
@Path("/doGet/{first}/{last}")
@Produces(MediaType.APPLICATION_XML)
public String doGet(@PathParam(value="first") String firstName,@PathParam(value="last") String lastName); @POST
@Path("/itemComfirm")
@Produces(MediaType.APPLICATION_XML)
public String itemComfirm(String xmlParam,@Context HttpServletRequest req,@Context HttpServletResponse res);
}

然后在projectImpl的pom.xml修改,这里加入对testInterface子项目的依赖:

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>testParent</groupId>
<artifactId>testParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>projectImpl</artifactId>
<packaging>war</packaging> <dependencies>
<dependency>
<groupId>testParent</groupId>
<artifactId>testInterface</artifactId>
<version>122</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

然后在projectImpl的src/main/java下面新建类,并且实现testInterface子项目中的TestIn1接口:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class TestIn1Impl implements TestIn1 { public String doGet(String firstName, String lastName) { return "hihihihi"+","+firstName+","+lastName;
} public String itemComfirm(String xmlParam, HttpServletRequest req, HttpServletResponse res) { return "hahahaha"+","+xmlParam;
} }

再然后在下图路径修改application.xml和web.xml,没有文件的话就自己新建:

application.xml:

jaxrs:extensionMappings和jaxrs:languageMappings表示可根据URL后缀不同来进行不同的配置,例如/testURL.xml,那么该请求的请求头就会设置为application/xml,为/testURL.en,则会用en-gb来解析请求内容。详情可参考:http://cxf.apache.org/docs/jax-rs.html

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd"> <bean id="TestIn1Impl" class="TestIn1Impl"/>
<jaxrs:server id="TestIn1ImplServiceContainer">
<jaxrs:serviceBeans>
<ref bean="TestIn1Impl" />
</jaxrs:serviceBeans> <jaxrs:extensionMappings>
<entry key="xml" value="application/xml" />
</jaxrs:extensionMappings> <jaxrs:languageMappings>
<entry key="en" value="en-gb"/>
</jaxrs:languageMappings>
</jaxrs:server> </beans>

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"
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>fsp-api</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:application.xml</param-value>
</context-param> <!-- spring context listener -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- CXF -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/testParent/*</url-pattern>
</servlet-mapping> </web-app>

然后再写一个测试类:

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; public class TestClient1 { private WebClient client;
private String url="http://localhost:8080/testParent/testParent/a"; @Before
public void setUp() throws Exception{
System.out.println("setupbegan");
client=WebClient.create(url).header("charset", "UTF-8").encoding("UTF-8").acceptEncoding("UTF-8");
} @After
public void tearDown() throws Exception{
System.out.println("tearDownbegan");
client=null;
} @Test
public void testItemConfirm(){
System.out.println("began");
String xmlaaParam="<a>ddd</a>";
String responseMsg=client.path("itemComfirm").accept(MediaType.APPLICATION_XML).post(xmlaaParam,String.class);
System.out.println("response :"+responseMsg);
System.out.println("end");
} @Test
public void testDoGet() {
System.out.println("began");
String responseString = client.path("doGet/"+"dsdsd"+"/"+"uyuy"+"")
.accept(MediaType.APPLICATION_XML)
.get(String.class);
System.out.println("response :"+responseString);
System.out.println("end");
} }

最后,在Eclipse的sever插件中加入ProjetImpl项目,然后启动项目:

启动成功之后,执行测试类,控制台输出:

setupbegan
began
response :hihihihi,dsdsd,uyuy
end
tearDownbegan
setupbegan
began
response :hahahaha,<a>ddd</a>
end
tearDownbegan

如有错误,欢迎指出。

转载请注明出处:http://www.cnblogs.com/Starshot/p/6889751.html

(原创)Maven+Spring+CXF+Tomcat7 简单例子实现webservice的更多相关文章

  1. idea+maven+spring+cxf创建webservice应用(二)生成客户端程序

    idea+maven+spring+cxf创建webservice应用(二)生成客户端程序,以上一篇为基础"idea+maven+spring+cxf创建webservice应用" ...

  2. c# spring aop的简单例子

    刚刚完成了一个c#的spring aop简单例子,是在mac下用Xamarin Studio开发的.代码如下: 接口 using System; using System.Collections.Ge ...

  3. maven+springmvc+cxf 实现简单webservice小例子

    1.首先你需要创建一个maven项目[当然是web项目] 2.pom.xml添加以下 <properties> <cxf.version>2.2.3</cxf.versi ...

  4. Eclipse+Maven+Spring+CXF 构建webservice 服务

    一. 软件准备 Eclipse 4.2.1 Maven 2.2.1 Spring 3.2.6 CXF 3.0.2 二. 步骤 首先,在Eclipse中用maven构建一个quickstart版本的ma ...

  5. MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建

    : 一:安装软件:tomcatZookeeperDubbo+admin 二:工程: 总工程  API    Pom.xml:不用引用任何东西  Provider    Pom.xml:要denpend ...

  6. spring容器BeanFactory简单例子

    在Spring中,那些组成你应用程序的主体及由Spring Ioc容器所管理的对象,都被称之为bean.简单来讲,bean就是Spring容器的初始化.配置及管理的对象.除此之外,bean就与应用程序 ...

  7. Maven配置dubbo环境简单例子

    环境准备: 1.zookeeper:zookeeper-3.4.6版本 2.maven:apache-maven-3.3.9版本 3.dubbo监控工具:dubbo-admin-2.5.4-SNAPS ...

  8. 使用 CXF 做 webservice 简单例子(转载)

    使用 CXF 做 webservice 简单例子     Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这 ...

  9. Spring框架系列(2) - Spring简单例子引入Spring要点

    上文中我们简单介绍了Spring和Spring Framework的组件,那么这些Spring Framework组件是如何配合工作的呢?本文主要承接上文,向你展示Spring Framework组件 ...

随机推荐

  1. C++ struct 初始化的问题

    struct student { int age; string name; int id; }; 初始化: student st1={10, "li ming", 01}; 修改 ...

  2. 少年,是时候换种更优雅的方式部署你的php代码了

    让我们来回忆下上次你是怎么发布你的代码的: 1. 先把线上的代码用ftp备份下来 2. 上传修改了的文件 3. 测试一下功能是否正常 4. 网站500了,赶紧用备份替换回去 5. 替换错了/替换漏了 ...

  3. 设置spring-boot的logging

    spring-boot默认使用logback来记录logger,spring-boot的包里面org.springframework.boot.logging.logback路径下面有一些配置文件,默 ...

  4. LinkCode 第k个排列

    http://www.lintcode.com/zh-cn/problem/permutation-sequence/# 原题 给定 n 和 k,求123..n组成的排列中的第 k 个排列. 注意事项 ...

  5. 关于特殊文件权限:suid、sgid和sticky-bit

    用 ls –l 命令时,能看到三个八进制数字,表示文件的权限.其实文件的权限应该用4个八进制文件来表示,没有显示的那个是第一位,用来设定一些特殊的权限,这个八进制数字的三个位是:SUID.SGID.s ...

  6. [uva11992]Fast Matrix Operations(多延迟标记,二维线段树,区间更新)

    题目链接:https://vjudge.net/problem/UVA-11992 题意:n*m的矩阵,每次对一个子矩阵操作,有三种操作:加x,设置为x,查询.查询返回子矩阵和.最小值.最大值 n很小 ...

  7. 搭建自己的Git服务器

    前言: GitHub是一个免费托管开源代码的远程仓库,使用起来即方便又安全,但在国内有时访问巨慢,原因你懂得.还有一些公司和个人视源码如生命,既不想开源代码又不想给Github交保护费.这时搭建一个自 ...

  8. virtualbox中实施ASM扩容实施记录

    实施ASM在线扩容 1. 首先关闭集群 [root@node1 bin]# ./crsctl stop crs [root@node2 bin]# ./crsctl stop crs 关机,halt ...

  9. .Net程序员学用Oracle系列(23):视图理论、物化视图

    1.视图理论 1.1.视图的存储 1.2.视图的作用 1.3.视图的工作机制 1.4.视图的依赖性 1.5.可更新的连接视图 1.6.内联视图 2.物化视图 2.1.刷新物化视图 2.2.物化视图日志 ...

  10. zabbix安装配置

    实验环境 主机名 操作系统版本 IP地址 安装软件 console CentOS 7.0 114.55.29.246 Httpd.Nginx.MySQL.Zabbix log1 CentOS 7.0 ...