简单的WebServiceCXF配置
一、 WebService服务端:
1) 添加以下jar包:
2) 添加以下类:
接口ISayHello.java
@WebService //这个不能少
public interface ISayHello {
public Stringhello(String args);
}
实现类SayHelloImpl.java
public class SayHelloImp implements ISayHello {
public String hello(Stringargs) {
return "success!!!" + args;
}
}
3) 添加Spring的配置文件applicationContext_cxf.xml:
<?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:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd">
<!--导入与CXF框架有关的xml -->
<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"/>
<!-- 发布WebService接口 -->
<jaxws:endpoint id="sayHello1"
implementor="com.mei.service.SayHelloImp"address="/cxfTest" />
</beans>
4) 修改web.xml文件:
<servlet>
<servlet-name>WebServiceCXF</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name> WebServiceCXF</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
在地址栏里输入http://localhost:8664/WebServiceCXF/services/,如果出现以下页面则发布成功
二、 WebService客户端
1) 添加以下jar包:
2) 添加接口,类名与服务端类名最好相同(包名可不同):
@WebService //这个不能少
public interface ISayHello {
public Stringhello(String args);
}
3) 添加测试类:
public static void main(String[] args) {
try {
String url = "http://localhost:8664/WebServiceCXF/services/cxfTest";
JaxWsProxyFactoryBean factory= new JaxWsProxyFactoryBean();
factory.setAddress(url);
factory.setServiceClass(ISayHello.class);
factory.create();
ISayHello result =(ISayHello)factory.create();
System.out.println(result.hello("WebServiceCXF").toString() + " -------------");
} catch (Exception e) {
e.printStackTrace();
}
}
运行后控制台输出以下信息:
注:如果返回的是JavaBean及List,参照文档WebService_CXF2