CXF是目前最流行的webService框架,今天写了一个小demo,还是以HelloWorld开头,的源码在附件.
服务端:工程名取名为WebService,该下的包去官方下cxf包。
一、建一个实体类User和一个HelloWorld接口及 HelloWorld接口实现类 HelloWorldImpl ,包名是com.lyqf:
User:
package com.lyqf; public class User { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge( int age) { this.age = age; } }
HelloWorld接口:
package com.lyqf; import javax.jws.WebService; @WebService public interface HelloWorld { String sayHi(String text); int getNum( int num); User getUsers(); }
HelloWorldImpl实现类:
package com.lyqf;
public class HelloWorldImpl implements HelloWorld{
public String sayHi(String text) {
// TODO Auto-generated method stub
return "Hi,"+text;
}
public int getNum(int num) {
// TODO Auto-generated method stub
return num;
}
public User getUsers() {
User user = new User();
user.setAge(13);
user.setName("李导");
return user;
}
}
二、web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
<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>
三、在web-inf下面新建一个beans.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/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.lyqf.HelloWorldImpl" address="/HelloWorld" />
</beans>
四、然后发布到tomcat上面: 看结果。
客用端:新建一个web项目(java项目)把cxf包全导进来,
现在都cxf都自带wsdl2java工具,有两种方法来做:
1、编写bulid.xml文件,内容是:
<?xml version="1.0" encoding="UTF-8"?>
<project name="cxf wsdl2java" basedir=".">
<property name="cxf.home" location ="${basedir}/WebRoot/WEB-INF/"/>
<path id="cxf.classpath">
<fileset dir="${cxf.home}/lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="cxfWSDLToJava">
<java classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="true">
<arg value="-client"/>
<arg value="-d"/>
<arg value="src"/>
<arg value="http://localhost:8080/WebService/HelloWorld?wsdl"/>
<classpath>
<path refid="cxf.classpath"/>
</classpath>
</java>
</target>
</project>
注:小白可能不懂,其实直接在bulid.xml右键ant就就可以执行了,然后F5刷新一下,src下面就有很多java代码了。
2、通过配置像JAVA_HOME来做,比如取名叫:JAVA_CXF,然后找到他的位置,再通过path找到cxf的bin目录。网上很多教程。
新建一个Client类,这个就是客户端的测试:
package com.lyqf;
public class Client {
public static void main(String[] args) {
HelloWorldImplService fatory = new HelloWorldImplService();
//这里是返回的是接口代理
HelloWorld hw = fatory.getHelloWorldImplPort();
User us = hw.getUsers();
System.out.println(us.getName()+" "+us.getAge());
}
}
输出看下结果。
所用的lib:http://down.51cto.com/data/779187