myeclipse xifre
1.首先新建一个web工程CxfService,倒入cxf所学要的包。要倒入的包如下:
commons-logging-1.1.jar
geronimo-activation_1.1_spec-1.0-M1.jar(orSun'sActivationjar)
geronimo-annotation_1.0_spec-1.1.jar(JSR250)
geronimo-javamail_1.4_spec-1.0-M1.jar(orSun'sJavaMailjar)
geronimo-servlet_2.5_spec-1.1-M1.jar(orSun'sServletjar)
geronimo-ws-metadata_2.0_spec-1.1.1.jar(JSR181)
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
TheSpringjars(optional-forXMLConfigurationsupport):
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
AndtheCXFjar:
cxf-2.1.jar
2.新建一个接口类:HelloWorld,如下:
Java代码
Java代码
packagecom.zx.cxf.service;
importjavax.jws.WebParam;
importjavax.jws.WebService;
@WebService
publicinterfaceHelloWorld{
StringsayHi(@WebParam(name="text")Stringtext);
}
创建接口的实现类:HelloWorldImpl,如下
Java代码
Java代码
packagecom.zx.cxf.service;
importjavax.jws.WebService;
importcom.zx.cxf.service.HelloWorld;
@WebService(endpointInterface="com.zx.cxf.service.HelloWorld",
servicename="HelloWorld")
publicclassHelloWorldImplimplementsHelloWorld{
publicStringsayHi(Stringtext){
return"Hello"+text;
}
}
*@WebService:申明为webservice的注解
*endpointInterface:要暴露的接口类
*serviceName:服务名
在WEB-INF目录下新建beans.xml,如下:
Xml代码
Xml代码
<?xmlversion="1.0"encoding="UTF-8"?>
<!--STARTSNIPPET:beans-->
<beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd">
<importresource="classpath:META-INF/cxf/cxf.xml"/>
<importresource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<importresource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<jaxws:endpoint
id="helloWorld"
implementor="com.zx.cxf.service.HelloWorldImpl"
address="/HelloWorld"/>
</beans>
<!--ENDSNIPPET:beans-->
注:implementor:接口类的实现类
address:要和注释里面神秘的服务名对应,
修改web.xml文件,如下:
Xml代码
Xml代码
<?xmlversion="1.0"encoding="ISO-8859-1"?>
<!DOCTYPEweb-app
PUBLIC"-//SunMicrosystems,Inc.//DTDWebApplication2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<!--STARTSNIPPET: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>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>/services/*</url-pattern>
</servlet-mapping>
</web-app>
<!--ENDSNIPPET:webxml-->
启动tomcat
测试:简单的测试就是ie测试,在浏览器中输入http://localhost:8080/CxfService/services/,如果出现
{http://service.cxf.zx.com/}HelloWorldImplPort,或者输入http://localhost:8080/CxfService/services/HelloWorld?wsdl,出现wsdl文挡,则说明服务器端配置成功。
可户端测试:
测试类如下:
Java代码
Java代码
packagecom.zx.cxf.service;
importorg.apache.cxf.jaxws.JaxWsProxyFactoryBean;
importorg.apache.cxf.interceptor.*;
importcom.zx.cxf.service.HelloWorld;
publicclassclient{
privateclient(){
}
publicstaticvoidmain(Stringargs[])throwsException{
JaxWsProxyFactoryBeanfactory=newJaxWsProxyFactoryBean();
factory.getInInterceptors().add(newLoggingInInterceptor());
factory.getOutInterceptors().add(newLoggingOutInterceptor());
factory.setServiceClass(HelloWorld.class);
factory.setAddress("http://localhost:8080/CxfService/services/HelloWorld");
HelloWorldclient=(HelloWorld)factory.create();
Stringreply=client.sayHi("HI");
System.out.println("Serversaid:"+reply);
}
}
如果控制台打印出:Serversaid:HelloHI则说明测试成功。
Ps:如果出现inthread"main"javax.xml.ws.soap.SOAPFaultException:ErrorreadingXMLStreamReader.
关掉防火墙就可以了。
下面是源文件:下载后倒入所需要的包
相关推荐
A.在myeclise中恢复例子开发环境:1>在myeclipse中新建一个web工程,项目名称自己任意取名,"J2EE Specification Level"一栏选择"J2EE1.4",然后点击"