项目里要弄一个用cxf做webservice开发。自己找了些资料写了个demo,在这总结下。
1、首先cxf的服务器demo示例:
a.新建一个web项目,命名webservice[img][/img]导入cxf所必须的包
b.开始cxf服务器端编写,首先在web.xml里加入cxf配置
<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>
其中beans.xml文件是放在WEB-INF下,在beans.xml中引入cxf的配置文件信息
<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.test.HelloWordImpl"
address="/HelloWorld" />
HelloWordImpl是webservice接口的实现类, address="/HelloWorld" 是webservice访问接口路径,客户端调用时需要和服务器端的访问端口保存一直。
c.编写客户端需要调用的webservice接口及实现类。
cxf采用声明式指定是否是webservice接口类
@WebService
public interface HelloWord {
public @WebResult String sayHello(String name);
}
接口的实现类同样也是
@WebService
public class HelloWordImpl implements HelloWord {
public String sayHello(String name) {
System.out.println("say hello");
return "hello12 "+" "+name+" ok!";
}
}
接口,实现类都完毕后,启动tomcat。在浏览器输入http://localhost:8080/WebService/HelloWorld?wsdl能够返回wsdl文件信息说明服务器端配置完成了。其中WebService是你项目的名称,HelloWorld是你在bean.xml在配置的address信息。
1、首先cxf的服务器demo示例:
a.新建一个web项目,命名webservice[img][/img]导入cxf所必须的包
b.开始cxf服务器端编写,首先在web.xml里加入cxf配置
<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>
其中beans.xml文件是放在WEB-INF下,在beans.xml中引入cxf的配置文件信息
<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.test.HelloWordImpl"
address="/HelloWorld" />
HelloWordImpl是webservice接口的实现类, address="/HelloWorld" 是webservice访问接口路径,客户端调用时需要和服务器端的访问端口保存一直。
c.编写客户端需要调用的webservice接口及实现类。
cxf采用声明式指定是否是webservice接口类
@WebService
public interface HelloWord {
public @WebResult String sayHello(String name);
}
接口的实现类同样也是
@WebService
public class HelloWordImpl implements HelloWord {
public String sayHello(String name) {
System.out.println("say hello");
return "hello12 "+" "+name+" ok!";
}
}
接口,实现类都完毕后,启动tomcat。在浏览器输入http://localhost:8080/WebService/HelloWorld?wsdl能够返回wsdl文件信息说明服务器端配置完成了。其中WebService是你项目的名称,HelloWorld是你在bean.xml在配置的address信息。