Spring整合JAX-WS

时间:2021-10-17 19:48:31

  Jax-ws在使用上很方便,也很轻量级。重点是他是jvnet(dev.java.net)的项目,是基于java标准的(JSR181)。

  不过它与Spring的整合相对麻烦,于此,我将自己的一些研究结果贴出来以供备忘和后来者参考。

  首先我们要有组件支持,包括三部分(我们需要他们的jar包):

    Spring

    jax-ws

    jaxws commons spring

  Spring就不用说了,如果大家使用MyEclipse的话就直接添加支持。

  jax-ws的jar包可以再网站上下载,或者搜索下载“jax-ws 2.2”。

  jaxws commons spring的jar包是中间件,这个可能不好下载,如果不适用maven的话可能就只能在网络上搜索下载“jaxws-spring-1.8”(包含不只一个jar包哦)。

  

  然后我们导入jar包并创建web项目。

  Spring整合JAX-WS   Spring整合JAX-WS

  然后编写一个测试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就是你的网站。

  Spring整合JAX-WS

于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>

欢迎您移步我们的交流群,无聊的时候大家一起打发时间:Spring整合JAX-WS

或者通过QQ与我联系:Spring整合JAX-WS

(最后编辑时间2016-02-22 10:07:57)