67,512
社区成员
发帖
与我相关
我的任务
分享
下载jar包
http://cxf.apache.org
1.接口
package com.cxf.service;
import javax.jws.WebService;
@WebService
public interface HelloWord
{
String sayHi(String text);
}
2.实现类
package com.cxf.service;
import javax.jws.WebService;
@WebService(endpointInterface = "com.cxf.service.HelloWord")
public class HelloWordImpl implements HelloWord
{
public String sayHi(String text){
return "Hi" + text;
}
}
3.web.xml配置
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</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>/*</url-pattern>
</servlet-mapping>
</web-app>
4.服务发布,需先写一个beans.xml(这里配置的是服务)
<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/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" />
<jaxws:endpoint
id="helloWorld"
implementor="com.cxf.service.HelloWordImpl"
address="http://localhost:8080/HelloWorld"/>
</beans>
5.如果通过spring结合方式访问还需要配置一个客户端文件(applications.xml)放在src下面
<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/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" />
<jaxws:client id="helloClient"
serviceClass="com.cxf.service.HelloWord"
address="http://localhost:8080/HelloWorld" />
</beans>
6.发布
启动tomact,在浏览器输入http://localhost:8080/HelloWorld?wsdl
看是否能看到wsdl文件
7.测试
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // your Spring ApplicationContext
HelloWord client = (HelloWord) context.getBean("helloClient");
String response = client.sayHi("jianglh");
System.out.println(response);
}
通过cxf 提供的客户端代理工厂类来访问
public static void main(String[] args)
{
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(HelloWord.class);
factory.setAddress("http://localhost:8080/HelloWorld");
HelloWord iHelloWorld = (HelloWord) factory.create();
System.out.println("invoke webservice...");
System.out.println("message context is:" + iHelloWorld.sayHi("Josen"));
//System.exit(0);
}
通过另外一种方式访问
private static final QName SERVICE_NAME = new QName("http://service.cxf.com/","HelloWord");
private static final QName PORT_NAME = new QName("http://service.cxf.com/","HelloWordPort");
public static void main(String[] args)
{
Service service =Service.create(SERVICE_NAME);
String endpointAddress="http://localhost:8080/HelloWorld";
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
HelloWord hi=service.getPort(HelloWord.class);
String returnvalue = hi.sayHi("hello");
System.out.println(returnvalue);
}