Spring整合JAX-WS
Jax-ws在使用上很方便,也很轻量级。重点是他是jvnet(dev.java.net)的项目,是基于java标准的(JSR181)。
不过它与Spring的整合相对麻烦,于此,我将自己的一些研究结果贴出来以供备忘和后来者参考。
首先我们要有组件支持,包括三部分(我们需要他们的jar包):
Spring就不用说了,如果大家使用MyEclipse的话就直接添加支持。
jax-ws的jar包可以再网站上下载,或者搜索下载“jax-ws 2.2”。
jaxws commons spring的jar包是中间件,这个可能不好下载,如果不适用maven的话可能就只能在网络上搜索下载“jaxws-spring-1.8”(包含不只一个jar包哦)。
然后我们导入jar包并创建web项目。

然后编写一个测试WebService类:
package org.coderecord.blog; import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import org.springframework.stereotype.Service; /**
* 测试服务类<br>
* Service注解为在使用Spring的packeage-scan功能进行自动装配<br>
* WebService注解中可以不传递参数<br>
* SOAPBinding中也可不传递参数,或者按照自己的需求进行更改
*/
@Service("helloWorldService")
@WebService(targetNamespace = "org.coderecord.blog")
@SOAPBinding(style = Style.RPC)
public class HelloWorldService { /* 使用Spring来注入dao或service吧
@Autowired
private XXDao xxDao;*/ /**
* 接口方法必须加上WebMethod注解
*/
@WebMethod
public void sayHello() {
System.out.println("Hello World!");
}
}
HelloWorldService
然后修改applicationContext.xml,加上webservice的绑定,这里面有几个问题,对于wss和ws的schema必须加上:
<?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:ws="http://jax-ws.dev.java.net/spring/core"
xmlns:wss="http://jax-ws.dev.java.net/spring/servlet" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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.2.xsd
http://jax-ws.dev.java.net/spring/core
http://jax-ws.dev.java.net/spring/core.xsd
http://jax-ws.dev.java.net/spring/servlet
http://jax-ws.dev.java.net/spring/servlet.xsd"> <context:component-scan base-package="org.coderecord.blog">
</context:component-scan> <wss:binding url="/service/hello">
<wss:service>
<ws:service bean="#helloWorldService" />
</wss:service>
</wss:binding>
</beans>
applicationContext
最后修改web.xml,修改几个地方:
加上Spring的listener,并配置正确;
加上WSSpringServlet的拦截。
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Start WebService Config -->
<servlet>
<servlet-name>JAXWSServlet</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAXWSServlet</servlet-name>
<url-pattern>/service/hello</url-pattern>
</servlet-mapping>
<!-- End WebService Config -->
</web-app>
web
最后就启动,搞定。
访问localhost:8080/ExJaxwsSpring/service/hello?wsdl就是接口;访问localhost:8080/ExJaxwsSpring就是你的网站。

于2016-02-22:
有朋友说用新版Spring(Spring4.x)时产生“Caused by: java.lang.IllegalArgumentException: class com.sun.proxy.$Proxy5 has neither @WebSerivce nor @WebServiceProvider annotation”类似错误,这是由于“代理”类生成的子类(为了方便AOP)没有了@WebService注解。如果你不清楚Proxy和AOP,我举个栗子,你继承一个有注解的类,子类会继承父类的注解吗?
那么解决办法是在ws:service节点中加入一个impl属性约定继承的接口。例如
<wss:binding url="/service/hello">
<wss:service>
<ws:service bean="#helloWorldService" impl="org.coderecord.blog.HelloWorldService" />
</wss:service>
</wss:binding>
欢迎您移步我们的交流群,无聊的时候大家一起打发时间:
或者通过QQ与我联系:
(最后编辑时间2016-02-22 10:07:57)
Spring整合JAX-WS的更多相关文章
- CXF和spring整合遇到的问题:No bean named 'cxf' is defined
今天在做ws和spring整合的时候,很不幸的遇到了这个问题,百度了好久,竟然没人遇到这个问题,后来谷歌了一下,都是遇到这个问题的了...在看到一篇文章中提到了cxf.xml,所以我果断的打开这个配置 ...
- Spring整合CXF,发布RSETful 风格WebService(转)
Spring整合CXF,发布RSETful 风格WebService 这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有很大一部分都是一样的.关于发布CXF WebServer和Sp ...
- 13_CXF和Spring整合发布服务
[服务端] 第一步:建立一个Web项目 第二步:填充CXF jar包 第三步:创建接口及服务类 [工程截图(对比之前的WebService_CXF_Server00)] [applicationCon ...
- Spring整合CXF步骤,Spring实现webService,spring整合WebService
Spring整合CXF步骤 Spring实现webService, spring整合WebService >>>>>>>>>>>> ...
- Spring整合CXF,发布RSETful 风格WebService
原文地址:http://www.cnblogs.com/hoojo/archive/2012/07/23/2605219.html 这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有 ...
- 【WebService】WebService之CXF和Spring整合(六)
前面介绍了WebService与CXF的使用,项目中我们经常用到Spring,这里介绍CXF与Spring整合 步骤 1.创建一个Maven Web项目,可以参照:[Maven]Eclipse 使用M ...
- Java WebService 教程系列之 Spring 整合 CXF
Java WebService 教程系列之 Spring 整合 CXF 一.引入 jar 包 <dependency> <groupId>org.apache.cxf</ ...
- 【jersey】 spring 整合jersey 实现RESTful webservice
Jersey是一个RESTFUL请求服务JAVA框架,与常规的JAVA编程使用的struts框架类似,它主要用于处理业务逻辑层.与Struts类似,它同样可以和hibernate,sprin ...
- 30Mybatis_mybatis和spring整合-原始dao开发
这篇文章很重要, 第一步:我们讲一下整合的思路: 我们以前要用Mybatis是需要sqlMapConfig.xml(这个配置文件需要配置dataource,以及mapper.xml文件.)sqlMap ...
- Spring整合WebSocket
WebSocket,干什么用的?我们有了HTTP,为什么还要用WebSocket?很多同学都会有这样的疑问.我们先来看一个场景,大家的手机里都有微信,在微信中,只要有新的消息,这个联系人的前面就会有一 ...
随机推荐
- [Xamarin] 用Service 來製作一個Notification的時鐘 (转帖)
這篇利用來製作一個會出現在Notification的時鐘,來敘述一下 Service,在你製作的App被關閉時,可以透過Service繼續運行你想處理的部分,當然Service 也有其生命周期 接下來 ...
- centos7 安装 notejs
1.安装集成工具 yum -y install gcc make gcc-c++ 2.安装notejs 自行选择版本:https://nodejs.org/dist/ wget https://nod ...
- EmberJs之3W
写在前面 最近比较忙,换了新工作还要学习很多全新的技术栈,并给自己找了很多借口来不去坚持写博客.常常具有讽刺意味的是,更多剩下的时间并没有利用而更多的是白白浪费,也许这就是青春吧,挥霍吧,这不是我想要 ...
- 微信支付接口 H5
php微信支付若干问题记录 1.缺少参数$key0$ 此问题的可能性有几种,大致有1.timeStamp这个参数应该是string类型,默认time是int 2.确实是参数缺少 比如:prepay_ ...
- 【Leetcode】【Medium】Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- maven 打包 xml文件
说起来手贱啊,搞了两个小时,就是因为打包的时候,maven无法把xml文件打包到正确的位置. 本来应该是打包到 com/presistence包下,结果打出来有两个包,一个是com/presisten ...
- 分享我用Qt开发的应用程序【二】在Qt应用程序中使用字体图标fontawesome
为了使用简单,需要先写一个单件类,头文件的代码如下: 其中静态方法Instance保证IconHelper的实例全局唯一 (注意构造函数已经私有化了) #ifndefICONHELPER_H #def ...
- Hive 安装配置
系统:Ubuntu 64 bit系统 step1:创建hive文件目录,并在hive目录下创建tmp,warehouse 和 log目录: Step2:解压hive安装包: Step3:创建配置文件: ...
- LEA指令
格 式:LEA OPRD1,OPRD2 功 能:将有效地址传送到指定的的寄存器 OPRD1 为目的操作数,可为任意一个16位的通用寄存器. OPRD2 为源操作数,可为变量名.标号或地址表 ...
- html中的图片直接使用字符串代替
最近来了一个网页,里面有图片,但是却没有引用外部的图片资源,很好奇.查看代码后发现,里面的图片是使用base64编码后的字符串代替了,这个叫做Data URI scheme. Data URI sch ...