1、关于spring不多说,用到cxf插件,则需要导入一些列jar包,我这里用的是maven管理jar包,在pom.xml文件中的maven依赖如下:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- Jetty is needed if you're are not using the CXFServlet -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
<properties>
<cxf.version>2.2.3</cxf.version>
</properties>
2、spring配置文件:
在该配置文件<beans>中首先要额外加上这些链接:xmlns:jaxws="http://cxf.apache.org/jaxws" 和
xsi:schemaLocation=" 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="hello" class="com.gstd.service.HelloWorldImpl" /> --一个接口的实现了。
<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /> --implementor 是bean中的id;address是暴漏出来的地址。
<!-- 上面和下面两种方式都可以
<jaxws:server id="helloWorld" serviceClass="#hello" address="/HelloWorld">
<jaxws:serviceBean>
<ref bean="hello"/>
</jaxws:serviceBean>
</jaxws:server>
-->
在配置spring文件之前,先写一个接口,然后写一个实现该接口的类,来个简单的实现就可。如下:
package com.gstd.service;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding.Style;
@WebService(targetNamespace="gstd")//如果不写targetNamespace的值时,默认是包反转,比如服务器项目中包是com.gstd.hw,那么默认值为hw.gstd.com,如果在另外
//的项目客户端中调用,则创建接口类HelloWorld时,类名可以不一样,但是targetNamespace必须一样。不然调用不成功!最好自己定义一个名称
@SOAPBinding(style = Style.RPC)
public interface HelloWorld {
String sayHi(String text);
}
package com.gstd.service;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding.Style;
@WebService(endpointInterface = "com.gstd.service.HelloWorld")这里指定服务的接口类的路径,也可以不写
@SOAPBinding(style = Style.RPC)
public class HelloWorldImpl implements HelloWorld {
public String sayHi(@WebParam(name = "text")String text)
{
System.out.println("sayHi called");
return "Hello " + text;
}
}
3、其次web.xml配置如下
<servlet>
<servlet-name><span><span class="tag"></span><span>springMVC</span><span class="tag"></span></span></servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:oa_mvc_ctx.xml</param-value>---项目启动时加载spring配置文件
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name><span><span class="tag"></span><span>springMVC</span><span class="tag"></span></span></servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置cxfservlet -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXFServlet</display-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>/webservice/*</url-pattern>
</servlet-mapping>
4、最后输入地址,验证webService是否发布成功:http://localhost:8080/项目名称/webservice/HelloWorld?wsdl
会出现xml的文档树,说明发布成功。
5、更好地文章比较全的连接推荐
http://www.cnblogs.com/hoojo/tag/WebService/