最近项目中用到WebService,搜索了一番,发现Jaxws与Spring结合是一种不错的方案,然而使用Jaxws官方jar包与Spring整合时(jdk6、Tomcat6),启动报错,初步判断可能是jar包冲突造成的,试了很久也没找到具体原因。后来发现使用MyEclipse自带的Jaxws插件的jar包可完美使用,以此文作为学习记录,也希望给你带来帮助!
一、项目概况
使用 JDK1.6、Tomcat6.0、Spring3.2,结构及jar包如下图:
二、代码
先声明一个简单的接口:
public interface IWelcome {
String say(String name);
}
实现接口,声明为Spring组件:
@Component("welcome")
public class WelcomeImpl implements IWelcome {
@Override
public String say(String name) {
return "Welcome," + name + "!";
}
}
WebService服务类:
@Service("welcomeService")
@WebService
@SOAPBinding(style = Style.RPC)
public class Welcome {
@Resource(name = "welcome")
private IWelcome welcome;
@WebMethod
public String sayWelcome(String name) {
return welcome.say(name);
}
}
三、配置文件
Spring配置文件applicationContext.xml,注意添加WebService相关名空间和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" <span style="background-color: rgb(255, 255, 153);">xmlns:ws="http://jax-ws.dev.java.net/spring/core"</span>
<span style="background-color: rgb(255, 255, 153);">xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"</span> 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
<span style="background-color: rgb(255, 255, 153);">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</span>">
<context:component-scan base-package="hyacinth.ws" />
<wss:binding url="/service/welcome">
<wss:service>
<ws:service bean="#welcomeService" />
</wss:service>
</wss:binding>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>JaxwsSpringDemo</display-name>
<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/welcome</url-pattern>
</servlet-mapping>
<!-- End WebService Config -->
</web-app>
四、至此,WebService服务完成,发布到Tomcat,访问 http://localhost:8080/JaxwsSpringDemo/service/welcome?wsdl 试试看吧
五、编写客户端
使用wsimport指令生成客户端,如 wsimport -s src -p test.ws.client http://localhost:8080/JaxwsSpringDemo/service/welcome?wsdl
打开CMD,进入项目根目录:
-s src表示在src文件夹下生成代码
-p 指定生成的代码所在的包
客户端测试代码:
public class Test {
public static void main(String[] args) {
WelcomeService welcomeService = new WelcomeService();
Welcome welcome = welcomeService.getWelcomePort();
System.out.println("start ws client...");
System.out.println(welcome.sayWelcome("Avatar"));
System.out.println("ws client ends...");
}
}
运行结果:
start ws client...
Welcome,Avatar!
ws client ends...
最后附上完整项目:http://download.csdn.net/detail/txwh_yj/8296367