下载cxf包,把他里面的包都添加进lib文件夹中。

创建一个接口。添加@WebService注解

@WebService
public interface HelloWorld { String sayHi(@WebParam(name="text")String text);
String sayHiToUser(User user);
String[] SayHiToUserList(List<User> userList);
}

创建接口的实现类,也添加@WebService注解,给他一个名称:boy

@WebService(serviceName="boy")
public class HelloWorldImpl implements HelloWorld { Map<Integer, User> users = new LinkedHashMap<Integer, User>(); public String sayHi(String text) {
return "Hello " + text;
} public String sayHiToUser(User user) {
users.put(users.size() + 1, user);
return "Hello " + user.getName();
} public String[] SayHiToUserList(List<User> userList) {
String[] result = new String[userList.size()];
int i = 0;
for (User u : userList) {
result[i] = "Hello " + u.getName();
i++;
}
return result;
} }

User实体类:

public class User {

    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;
} }

服务端代码:

public class webServiceApp {
public static void main(String[] args) {
System.out.println("web service start");
HelloWorldImpl implementor= new HelloWorldImpl();
String address="http://localhost:8000/helloWorld";
Endpoint.publish(address, implementor);
System.out.println("web service started");
}
}

客户端代码:有集成spring和没集成的

public class HelloWorldClient {
public static void main(String[] args) { //集成spring和cxf
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld client1 = (HelloWorld) context.getBean("client1");
User user1 = new User();
user1.setName("Tony");
user1.setAge(20);
User user2 = new User();
user2.setName("freeman");
user2.setAge(50);
List<User> userList = new ArrayList<User>();
userList.add(user1);
userList.add(user2);
// String[] res = client1.SayHiToUserList(userList);
// System.out.println(res[0]);
// System.out.println(res[1]);
String sayHi = client1.sayHi("good");
System.out.println(sayHi); // HelloWorld2 client2 = (HelloWorld2) context.getBean("client2");
// User user3 = new User();
// user3.setName("Jerry");
// user3.setAge(20);
// String user = client2.sayHiToUser(user3);
// System.out.println(user); //没有集成
JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
svr.setServiceClass(HelloWorld.class);
svr.setAddress("http://localhost:8000/helloWorld");
HelloWorld hw = (HelloWorld) svr.create();
User user = new User();
user.setName("Tony");
System.out.println(hw.sayHi("ffff")); }
}

打印的结果是:

Hello good

Hello ffff

web.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>cxf</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param> <listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener> <listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</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>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

applicationContext.xml的配置,这个文件是放在资源文件resources下的,他在项目中的路径是在classes里面的。我在这里配置了两个站点。都是可以的。

<?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" 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"> <!-- 发布一个站点 -->
<jaxws:endpoint id="boy" implementor="com.cs.webservice.HelloWorldImpl"
address="/aaa" /> <bean id="client1" class="com.cs.webservice.HelloWorld"
factory-bean="clientFactory1" factory-method="create" /> <bean id="clientFactory1" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.cs.webservice.HelloWorld" />
<property name="address" value="http://localhost:8000/helloWorld/aaa" />
</bean> <!-- 第二个站点 -->
<!-- <jaxws:endpoint id="orange" implementor="com.cs.webservice.HelloWorldImpl2"
address="/bbb" /> <bean id="client2" class="com.cs.webservice.HelloWorld2"
factory-bean="clientFactory2" factory-method="create" /> <bean id="clientFactory2" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.cs.webservice.HelloWorld2" />
<property name="address" value="http://localhost:8080/cxfwebservicespring/bbb" />
</bean> -->
</beans>
<property name="address" value="http://localhost:8000/helloWorld/aaa" />
aaa前面的路径必须与发布的String address="http://localhost:8000/helloWorld";路径相同。

webservice整合spring cxf的更多相关文章

  1. WebService之Spring+CXF整合示例

    一.Spring+CXF整合示例 WebService是一种跨编程语言.跨操作系统平台的远程调用技术,它是指一个应用程序向外界暴露一个能通过Web调用的API接口,我们把调用这个WebService的 ...

  2. CXF WebService整合Spring

    转自http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html 首先,CXF和spring整合需要准备如下jar包文件: 这边我是用Spr ...

  3. webservice整合spring

    接口HelloWorld需要添加webservice注解 package com.cs.webservice; import java.util.List; import javax.jws.WebP ...

  4. webservice 服务端例子+客户端例子+CXF整合spring服务端测试+生成wsdl文件 +cxf客户端代码自动生成

    首先到CXF官网及spring官网下载相关jar架包,这个不多说.webservice是干嘛用的也不多说. 入门例子 模拟新增一个用户,并返回新增结果,成功还是失败. 大概的目录如上,很简单. Res ...

  5. WebService学习之三:spring+cxf整合

    步骤一:spring项目(java web项目)引入CXF jar包 步骤二:创建webservice服务器 1)创建一个服务接口 package com.buss.app.login; import ...

  6. CXF整合Spring开发WebService

    刚开始学webservice时就听说了cxf,一直没有尝试过,这两天试了一下,还不错,总结如下: 要使用cxf当然是要先去apache下载cxf,下载完成之后,先要配置环境变量,有以下三步: 1.打开 ...

  7. Spring整合CXF步骤,Spring实现webService,spring整合WebService

    Spring整合CXF步骤 Spring实现webService, spring整合WebService >>>>>>>>>>>> ...

  8. Spring+CXF整合来管理webservice(服务器启动发布webservice)

    Spring+CXF整合来管理webservice    实现步骤:      1. 添加cxf.jar 包(集成了Spring.jar.servlet.jar ),spring.jar包 ,serv ...

  9. WebService—CXF整合Spring实现接口发布和调用过程

    一.CXF整合Spring实现接口发布 发布过程如下: 1.引入jar包(基于maven管理) <!-- cxf --> <dependency> <groupId> ...

随机推荐

  1. iOS-延迟操作方法总结

    在实际应用中,有时候我们会需要延时执行某些操作,所以我们这里总结了四种延迟操作的方法,并简要分析了每种方法的异同. NSObject的相关方法 第一种方法是使用NSObject类的performSel ...

  2. 敏捷开发(十)- Scrum每日例会

    本文主要是为了检测你对SCRUM 评估会议的了解和使用程度, 通过本文你可以检测一下     1.你们的SCRUM 没人例会的过程和步骤    2.SCRUM 每日例会的输出结果一.会议目的      ...

  3. [HMLY]5.模仿喜马拉雅 FM

    项目介绍: 文:HansRove(github)XiMaLaYa-by-HansRove- 仿做喜马拉雅, 对AVFoundation框架的一次尝试   软件环境: iOS9.1硬件环境: Mac O ...

  4. 去掉UItableview section headerview黏性

    UITabelView在style为plain时,在上拉是section始终粘在最顶上而不是跟随滚动而消失或者出现 可以通过设置UIEdgeInsetsMake: - (void)scrollView ...

  5. js实现多行文本超出一定字数显示省略号功能

    最近项目中遇到了一个关于超出一定字数用省略号显示的问题,其实这种形式很常见,公司简介.产品介绍啊里面都可能会用到,一行文字显示省略号很容易,多行就得想点办法了.在经过查阅.整理之后,我也算是实现了这个 ...

  6. Python学习笔记——基础篇【第六周】——logging模块

    常用模块之logging 用于便捷记录日志且线程安全的模块 import logging logging.basicConfig(filename='log.log', format='%(ascti ...

  7. spring @Scheduled 执行2次

    今天遇到定时任务Scheduled 执行2次的情况,做一个简单的记录. 网上有好多办法,我几乎都试了一遍,我的情况下面的办法可用. 1. autodeploy属性值设置为false,如果此项设为tru ...

  8. 在GNU/Linux下设置与定时更换桌面壁纸

    1 简介 在电脑桌面设置一组可以定时更换的壁纸已经不是什么新奇的功能了.比如,Windows 7.KDE桌面环境都可以实现这样的效果.可是,自己目前使用的Debian系统并未安装KDE.GNOME这样 ...

  9. 图片翻转(Raw Image)

    int TransformImageBuffer(unsigned char* pImageBuffer, int width, int height,unsigned char* targetIma ...

  10. java中float和double的区别

    float表示单精度浮点数在机内占4个字节,用32位二进制描述. double表示双精度浮点数在机内占8个字节,用64位二进制描述.浮点数在机内用指数型式表示,分解为:数符,尾数,指数符,指数四部分. ...