依赖包

<!-- CXF Dependencies -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.0.1</version>
</dependency>

web.xml配置

web.xml中首先在contextConfigLocation中配置applicationContext-server.xml的路径,并配置CXFService,路径为/service/*,指定webservice的访问路径为localhost:8080/service/*

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/nanri/config/springConfig/applicationContext.xml,
classpath:com/nanri/config/webserviceConfig/applicationContext-server.xml
</param-value>
</context-param>
<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>/service/*</url-pattern>
</servlet-mapping>

applicationContext-server.xml配置

<?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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.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-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
-->
<bean id="userServiceBean" class="com.nanri.webservice.serviceTest2.ServiceImpl" /> <!-- 注意下面的address,这里的address的名称就是访问的WebService的name -->
<jaxws:server id="userService" serviceClass="com.nanri.webservice.serviceTest2.IService"
address="/Users">
<jaxws:serviceBean>
<!-- 要暴露的 bean 的引用 -->
<ref bean="userServiceBean" />
</jaxws:serviceBean>
</jaxws:server>
</beans>

服务器端Java类

包结构如下:

对象User

package com.nanri.webservice.serviceTest2;

public class User {
int id ;
String name = null;
String address = null;
String email = null;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}

服务接口IService

package com.nanri.webservice.serviceTest2;

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style = Style.RPC)
public interface IService {
public User getUserByName(@WebParam(name = "name") String name);
public void setUser(User user);
}

服务接口实现ServiceImpl

package com.nanri.webservice.serviceTest2;

import java.util.Date;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style; import org.springframework.stereotype.Repository; /**
* <b>function:</b> WebService传递复杂对象,如JavaBean、Array、List、Map等
*/
@WebService
@SOAPBinding(style = Style.RPC)
@SuppressWarnings("deprecation")
public class ServiceImpl implements IService {
public User getUserByName(@WebParam(name = "name") String name) {
User user = new User();
user.setId(new Date().getSeconds());
user.setName(name);
user.setAddress("china");
user.setEmail(name + "@test.com");
return user;
} public void setUser(User user) {
System.out.println("############Server setUser###########");
System.out.println("setUser:" + user);
}
}

客户端

SpringUsersWsClient

package com.nanri.webservice.serviceTest2;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class SpringUsersWsClient {
public static void main(String[] args) { // 调用WebService JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(IService.class);
factory.setAddress("http://localhost:8080/SpringProject/service/Users");
IService service = (IService) factory.create();
System.out.println("#############Client getUserByName##############");
User user = service.getUserByName("hoojo");
System.out.println(user);
user.setAddress("China-Guangzhou");
service.setUser(user);
}
}

访问 http://localhost:8080/SpringProject/service/Users?wsdl ,看到wsdl文件即说明配置成功。

Spring下使用开发webservice的更多相关文章

  1. Spring集成XFire开发WebService

    Spring是眼下最流行的JavaEE Framework,可是使用Spring的Spring-WS开发WebService却十分繁琐.XFire是一个简化WebService开发的开源项目.通过Sp ...

  2. Spring boot+CXF开发WebService

    最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...

  3. Spring boot+CXF开发WebService Demo

    最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...

  4. struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例

    Cxf + Spring+ myeclipse+ cxf 进行  Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...

  5. MyEclipse下XFire开发Webservice实例

    XFire Java SOAP框架概述 (摘自:http://tech.it168.com/j/e/2006-10-28/200610281432707.shtml )     MyEclipse W ...

  6. Spring注解+Axis2开发WebService

    用Spring注解方式: 配置扫描指定包下的类 <context:component-scan base-package="包名" />   标识类为spring管理的 ...

  7. Spring Boot+CXF搭建WebService(转)

    概述 最近项目用到在Spring boot下搭建WebService服务,对Java语言下的WebService了解甚少,而今抽个时间查阅资料整理下Spring Boot结合CXF打架WebServi ...

  8. JAX-WS + Spring 开发webservice

    通过几天的时间研究了下使用jax-ws来开发webservice,看了网上的一些资料总结出jax-ws的开发大概分为两种. 以下项目使用的spring3.0,jar包可以到官网下载 第一种:使用独立的 ...

  9. spring集成环境下的axis webservice的发布,调试

    在spring集成的环境下,无论你是ssh集成,还是ssi集成的情况下,发布webservice往往在调用的时候会出错. 特别是,如果你是这个方式: 将webservice打aar包,放到tomcat ...

随机推荐

  1. C语言__LINE__实现原理

    在test.c中写如下代码: 1 #include <stdio.h> 2 3 int main() 4 { 5     printf("line:%d\n", __L ...

  2. JS window.onload 和模拟document.ready.

    hhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhh ttttttttttttt 注意观察 事件执行的 先后顺序. 总的来说,window.onload()方法是必须等到页面内包括图片的 ...

  3. [BZOJ3772]精神污染 主席树上树+欧拉序

    3772: 精神污染 Time Limit: 10 Sec  Memory Limit: 64 MB Description 兵库县位于日本列岛的中央位置,北临日本海,南面濑户内海直通太平洋,中央部位 ...

  4. PostgreSQL timeline

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL基础知识与基本操作索引页     回到顶级页面:PostgreSQL索引页 关于timeline,有如下的说法 http ...

  5. 成都Uber优步司机奖励政策(4月24日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. CF1111E Tree 树链剖分,DP

    CF1111E Tree 过年了,洛咕还没爬这次的题,先放个CF的链接吧. 补个LG传送门. 对于每个询问点\(x\),设它的祖先即不能和它放在同一个集合中的点的个数为\(f[x]\),设\(dp[i ...

  7. 实现对象属性的lazy-loading(延迟加载)

    一.延迟加载器LazyLoader作用:       说到延迟加载,应该经常接触到,尤其是使用Hibernate的时候,本篇将通过一个实例分析延迟加载的实现方式.LazyLoader接口继承了Call ...

  8. msil 笔记

    public class TestImpl : ITest { // Properties public string Address { get { return "abc"; ...

  9. 微信小程序日记(一)

    一.基础知识(目录与配置) (1)标签 小程序的view相当于HTML的div标签一样,作占位 (2)每一个页面都需要在app.json里面注册,例如: { { "pages": ...

  10. 【Jmeter测试】接口请求完成后,查询数据库结果,检测数据存储是否正确

    Jmeter脚本逻辑 发送POST请求,把数据保存到数据库中 发讯数据库,数据库查询结果保存的变量中 使用BeanShell判断数据库查询结果 Jmeter脚本结构 第一个箭头指的是JDBC Conn ...