myeclipse开发基于cxf的webservice

oldboy 2012-08-15

首先下载cxf包,目前最新的版本是apache-cxf-2.1.,下栽地址http://cxf.apache.org/download.html

 1. 首先新建一个web工程CxfService,倒入cxf所学要的包。要倒入的包如下:

commons-logging-1.1.jar
geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar)
geronimo-annotation_1.0_spec-1.1.jar (JSR 250)
geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar)
geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
jaxb-api-2.1.jar
jaxb-impl-2.1.6.jar
jaxws-api-2.1.jar
jetty-6.1.5.jar
jetty-util-6.1.5.jar
neethi-2.0.jar
saaj-api-1.3.jar
saaj-impl-1.3.jar
stax-api-1.0.1.jar
wsdl4j-1.6.1.jar
wstx-asl-3.2.1.jar
XmlSchema-1.2.jar
xml-resolver-1.2.jar

The Spring jars (optional - for XML Configuration support):

aopalliance-1.0.jar
spring-core-2.0.4.jar
spring-beans-2.0.4.jar
spring-context-2.0.4.jar
spring-web-2.0.4.jar

And the CXF jar:

cxf-2.1.jar
2.新建一个接口类:HelloWorld,如下:
Java代码 myeclipse开发基于cxf的webservice myeclipse开发基于cxf的webservicemyeclipse开发基于cxf的webservice
package com.zx.cxf.service;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface HelloWorld {
    String sayHi(@WebParam(name="text") String text);
}
 创建接口的实现类:HelloWorldImpl,如下
Java代码 myeclipse开发基于cxf的webservice myeclipse开发基于cxf的webservicemyeclipse开发基于cxf的webservice
package com.zx.cxf.service;

import javax.jws.WebService;

import com.zx.cxf.service.HelloWorld;
@WebService(endpointInterface = "com.zx.cxf.service.HelloWorld/*没有.java后缀*/",
            serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {
 
    public String sayHi(String text) {
        return "Hello " + text;
    }
}
*@WebService:申明为webservice的注解 
*endpointInterface:要暴露的接口类
 *serviceName :    服务名
在WEB-INF目录下新建beans.xml,如下:
Xml代码 myeclipse开发基于cxf的webservice myeclipse开发基于cxf的webservicemyeclipse开发基于cxf的webservice
<?xml version="1.0" encoding="UTF-8"?>

<!-- START SNIPPET: beans -->
<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.zx.cxf.service.HelloWorldImpl" <!--没有.java-->
	  address="/HelloWorld" />
	  
</beans>
<!-- END SNIPPET: beans -->
 注: implementor :接口类的实现类
        address:   要和注释里面神秘的服务名对应,
修改web.xml文件,如下:
Xml代码 myeclipse开发基于cxf的webservice myeclipse开发基于cxf的webservicemyeclipse开发基于cxf的webservice
<?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">

<!-- START SNIPPET: webxml -->
<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>/services/*</url-pattern>
	</servlet-mapping>
</web-app>
<!-- END SNIPPET: webxml -->
启动tomcat
测试:简单的测试就是ie测试,在浏览器中输入http://localhost:8080/CxfService/services/,如果出现

{http://service.cxf.zx.com/}HelloWorldImplPort ,或者输入http://localhost:8080/CxfService/services/HelloWorld?wsdl,出现wsdl文挡,则说明服务器端配置成功。

 可户端测试:

测试类如下:

Java代码 myeclipse开发基于cxf的webservice myeclipse开发基于cxf的webservicemyeclipse开发基于cxf的webservice
package com.zx.cxf.service;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.interceptor.*;
import com.zx.cxf.service.HelloWorld;
public  class client {

   

    private client() {
    } 

    public static void main(String args[]) throws Exception {
    	JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    	factory.getInInterceptors().add(new LoggingInInterceptor());
    	factory.getOutInterceptors().add(new LoggingOutInterceptor());
    	factory.setServiceClass(HelloWorld.class);
    	factory.setAddress("http://localhost:8080/CxfService/services/HelloWorld");
    	HelloWorld client = (HelloWorld) factory.create();

    	String reply = client.sayHi("HI");
    	System.out.println("Server said: " + reply);
    }

}

 如果控制台打印出:Server said: Hello HI则说明测试成功。

Ps:如果出现in thread "main" javax.xml.ws.soap.SOAPFaultException: Error reading XMLStreamReader.

关掉防火墙就可以了。

下面是源文件:下载后倒入所需要的包

相关推荐

tengmuxin / 0评论 2017-10-16