CXF webservice 一个简单的demo
新建一个maven项目(or下载cxf所需jar包),pom.xml如下
1.pom.xml
<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.webservice</groupId>
<artifactId>WebserviceProject</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>WebserviceProject Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-api</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-bindings-soap</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.5.0</version>
</dependency> </dependencies>
<build>
<finalName>WebserviceProject</finalName>
</build>
</project>
2.新建一个配置文件application.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <bean id="greetingServiceImpl" class="com.webservice.server.GreetingServiceImpl" >
<property name="excludeName" value="Michael"/>
<property name="leastPonit" value="10"/>
</bean>
<jaxws:endpoint id="greetingService" implementor="#greetingServiceImpl"
address="/Greeting" >
</jaxws:endpoint> </beans>
3.配置web.xml文件,加载xml和配置url
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name> <!--==这个设置很重要,那么我们的webservice的地址就是http://localhost:8080/webserviceProject/webservice/Greeting===-->
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
</web-app>
4 新建server实体类和接口
Greeting.java
package com.webservice.server;
import javax.jws.WebParam;
import javax.jws.WebService; import com.webservice.to.WebServiceTo;
@WebService
public interface Greeting {
public WebServiceTo greeting(@WebParam(name = "username")String username,@WebParam(name = "point")int point);
}
GreetingServiceImpl.java
package com.webservice.server; import javax.jws.WebService; import com.webservice.to.UserInfo;
import com.webservice.to.WebServiceTo; @WebService(endpointInterface = "com.webservice.server.Greeting")
public class GreetingServiceImpl implements Greeting { private String excludeName = "Michael";
private int leastPonit = 5; public WebServiceTo greeting(String username, int point) {
String result = "";
if (excludeName.equals(username)) {
result = " 您不能重复进行投票!";
} else {
result = " 谢谢您的投票!";
if (point < leastPonit) {
result += " 您的投票分数太低!";
} else {
result += " 您的投票分数通过审核!";
}
}
WebServiceTo to = new WebServiceTo();
to.setResult(result); UserInfo user = new UserInfo();
user.setUserName("zhangsan");
user.setPassword("abcd");
to.setInfo(user);
return to;
} public String getExcludeName() {
return excludeName;
} public void setExcludeName(String excludeName) {
this.excludeName = excludeName;
} public int getLeastPonit() {
return leastPonit;
} public void setLeastPonit(int leastPonit) {
this.leastPonit = leastPonit;
}
}
5.server端已经完成,接下来创建一个client端类Test
GreetingServiceClient.java
package com.webservice.client; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.webservice.server.Greeting; public class GreetingServiceClient {
public static void main(String[] args) {
// 创建WebService客户端代理工厂
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
// 注册WebService接口
factory.setServiceClass(Greeting.class);
// 设置WebService地址
factory.setAddress("http://localhost:8080/WebserviceProject/webservice/Greeting");
Greeting greetingService = (Greeting) factory.create();
System.out.println("invoke webservice...");
// 1、定义调查投票的变量与内容,用来发送给服务
String username = "Test";
int point = 88;
// 调用方法进行服务消费
String result = greetingService.greeting(username, point).getResult();
System.out.println("Result:" + result);
// 2、传递不一样的调查投票内容
username = "Michael";
point = 100;
// 再次调用方法进行服务消费,得到不一样的结果
result = greetingService.greeting(username, point).getResult();
System.out.println("Result:" + result);
// 3、第三次传递与调用
username = "Jordan";
point = 9;
result = greetingService.greeting(username, point).getResult();
System.out.println("Result:" + result);
}
}
CXF webservice 一个简单的demo的更多相关文章
- webService(简单小demo)
1.什么是webService? 1.1.先说好处: WebService是两个系统的远程调用,使两个系统进行数据交互,如应用: 天气预报服务.银行ATM取款.使用邮箱账号登录各网站等. WebSer ...
- MyBatis 学习记录1 一个简单的demo
主题 最近(N个月前)clone了mybatis的源码..感觉相比于spring真的非常小...然后看了看代码觉得写得很精简...感觉我的写代码思路和这个框架比较相似(很难具体描述...就是相对来说比 ...
- 无废话WCF入门教程六[一个简单的Demo]
一.前言 前面的几个章节介绍了很多理论基础,如:什么是WCF.WCF中的A.B.C.WCF的传输模式.本文从零开始和大家一起写一个小的WCF应用程序Demo. 大多框架的学习都是从增.删.改.查开始来 ...
- apache cxf之 一个简单的JAX-WS服务程序
推荐一本apache cxf的书籍: apache cxf的配置,这边就不做介绍了.请参照我关于它配置的博文. 开发步骤: 1.新建Java project,build path引入cxf runti ...
- [小北De编程手记] Lesson 01 - AutoFramework构建 之 从一个简单的Demo聊起
写在最前面 这个系列的主旨是要跟大家分享一下关于自动化测试框架的构建的一些心得.这几年,做了一些自动化测试框架以及团队的构建的工作.过程中遇到了很多这样的同学,他们在学习了某一门语言和一些自动化测试的 ...
- Spring Boot应用连接数据库MySQL、及一个简单的demo
一.修改pom.xml文件 在项目的pom.xml文件上增加如下代码,添加依赖文件. <dependency> <groupId>mysql</groupId> & ...
- 【转】WCF入门教程六[一个简单的Demo]
一.前言 前面的几个章节介绍了很多理论基础,如:什么是WCF.WCF中的A.B.C.WCF的传输模式.本文从零开始和大家一起写一个小的WCF应用程序Demo. 大多框架的学习都是从增.删.改.查开始来 ...
- 学习LSM(Linux security module)之二:编写并运行一个简单的demo
各种折腾,经过了一个蛋疼的周末,终于在Ubuntu14.04上运行了一个基于LSM的简单demo程序. 一:程序编写 先简单的看一下这个demo: //demo_lsm.c#include <l ...
- 一个简单的demo学习Android远程Service(AIDL的使用)
这是milo很早之前写在论坛上的一个帖子,现在整理出来,milo也复习一下一般来说Android 的四大组件都是运行在同一个进程中的,但远程Service运行在不同的进程里.这进程间的通信是使用了An ...
随机推荐
- linux之创建临时文件的方法
有时候,我们需要创建文件临时存放一些输出的信息,创建文件时就可能出现文件名存在的问题.如何创建唯一的文件名,Linux为我们提供几个方案: 1.mktemp(强烈推荐) The mktemp ut ...
- POJ 2168 Popular cows [Tarjan 缩点]
...
- Func<T1, T2, TResult> Delegate 系统Func委托类型
原文发布时间为:2011-03-25 -- 来源于本人的百度文章 [由搬家工具导入] http://msdn.microsoft.com/en-us/library/bb534647%28v=VS.1 ...
- FCKEDITOR配置说明
原文发布时间为:2009-10-12 -- 来源于本人的百度文章 [由搬家工具导入] fckeditor config.js配置2009-02-13 14:36 FCKConfig.CustomCon ...
- 手机横屏时候提示请竖屏浏览纯css实现
//今天无意间浏览nike公众号看到的 最近也正在做着就记录下来备忘<!DOCTYPE html> <html lang="en"> <head> ...
- MSP430 G2553 寄存器列表与引脚功能
USCI_B0 USCI_B0 发送缓冲器UCB0TXBUF 06Fh USCI_B0 接收缓冲器UCB0RXBUF 06Eh USCI_B0 状态UCB0STAT 06Dh USCI B0 I2C ...
- Educational Codeforces Round 34 B. The Modcrab【模拟/STL】
B. The Modcrab time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- TopCoder SRM 660 Div2 Problem 1000 Powerit (积性函数)
令$f(x) = x^{2^{k}-1}$,我们可以在$O(k)$的时间内求出$f(x)$. 如果对$1$到$n$都跑一遍这个求解过程,时间复杂度$O(kn)$,在规定时间内无法通过. 所以需要优化. ...
- 分享Kali Linux 2017年第18周镜像文件
分享Kali Linux 2017年第18周镜像文件 Kali Linux官方于4月30日发布2017年的第18周镜像.这次维持了11个镜像文件的规模.默认的Gnome桌面的4个镜像,E17.KD ...
- IIS 7 Access to the path ‘c:\windows\system32\inetsrv\’ is denied
https://randypaulo.wordpress.com/2011/09/13/iis-7-access-to-the-path-cwindowssystem32inetsrv-isdenie ...