1.创建web项目,需要的 jar 包
maven项目的pom文件
<!-- 属性定义 -->
<properties>
<cxf.version>2.7.1</cxf.version>
</properties>
<!-- 属性定义 --><!-- webService cxf beg -->
<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>
<!-- webService cxf end -->
因为我们在web项目里需要用到 CXFServlet ,所以需要引用 cxf-rt-transports-http-jetty 包,如果自己用main 方法测试,可以不用引用。
2.创建 IWebService 接口
package com.ws.webservice;
import javax.jws.WebMethod;
import javax.jws.WebService;@WebService
public interface IWebService {
@WebMethod
public String getValue(String name);
public String hello(String name);
}
3.创建 IWebService 接口的实现类
package com.ws.webservice.impl;
import com.ws.webservice.IWebService;
public class WebServiceImpl implements IWebService{
@Override
public String getValue(String name) {
return "你好!"+name;
}@Override
public String hello(String name) {
return "Hello World!";
}}
4.创建一个发布接口的测试类,测试一下 webservice接口
package com.ws.webservice.impl;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import com.ws.webservice.IWebService;
public class Launcher {
public static void main(String[] args) {
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setServiceClass(IWebService.class);
//接口的发布地址
factory.setAddress("http://localhost:9000/ws/"+IWebService.class.getSimpleName());
factory.setServiceBean(new WebServiceImpl());
factory.create();
}
}
到这一步,一个简单webservice接口就发布成功了。
通过浏览器访问 http://localhost:9000/ws/IWebService?wsdl 可以看到如下信息:
如果需要在web项目里发布接口,还需要以下几步
5.创建 context-cxf-servlet.xml 配置文件,整合spring
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
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://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><bean id="sendServiceBean" class="com.ws.webservice.impl.WebServiceImpl"/>
<jaxws:endpoint id="sendService" implementor="#sendServiceBean" address="/sendService"/>
</beans>
这个是通过 endpoint 端点服务的方式发布接口
其中,#sendServiceBean 是它上面定义的接口实现类的 bean 的名字,address="/sendService" 是我们需要访问的地址
6.配置web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/context*.xml
</param-value>
</context-param>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webService/*</url-pattern>/*匹配所有的service接口,context-cxf-servlet.xml文件内
</servlet-mapping>
第一个标红的地方:加载 context-cxf-servlet.xml 文件;第二个标红的地方:匹配所有的 service 接口,只要我们访问webservice/*这个地址就会进入CXF的servlet中
启动web项目,在浏览器中访问地址:http://localhost:8080/rwgl/webService/sendService?wsdl 看到如下内容:
则说明我们的webservice发布成功了。
接下来只需要通过客户端调用这个接口即可获得返回结果了。
总结
以上就是一个简单的webservice入门实例,更多的关于CXF拦截器以及其他的发布方式,客户端调用就没有做过多介绍,后续有时间的话再接着更新