1.使用的cxf版本2.6.0,tomcat6.0
2.必须引入的@R_404_5976@
3.新建两个web工程
a.命名为cxftest ------> 服务端
b.命名为cilient-------->客户端
先说服务端
<!-- spring begin--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-server.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- spring end --> <!-- cxf begin --> <servlet> <servlet-name>CXFService</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFService</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <!-- cxf end -->
自己写个接口用于测试
package com.wjl; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface UserService { public UserBean getUser(@WebParam(name = "name") String name); public void setUser(UserBean user); }其中@webservice一定不能少
定义一个userbean
public class UserBean { private int id; private String name; private String address; private String email;同时加入set get方法
下面完成接口的实现类
import java.util.Date; public class UserServiceImpl implements UserService { @Override public UserBean getUser(String name) { // Todo Auto-generated method stub UserBean user = new UserBean(); user.setId(new Date().getSeconds()); user.setName(name); user.setAddress("beijing"); user.setEmail(name + "@163.com"); return user; } @Override public void setUser(UserBean user) { // Todo Auto-generated method stub System.out.println("Server setUser!"); System.out.println("setUser:" + user); System.out.println("userName:" + user.getName()); System.out.println("userEmail:" + user.getEmail()); } }
下一步来完成spring配置文件
spring文件applicationContext,xml放在src目录下面即可
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.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"/> <bean id="userServiceBean" class="com.wjl.UserServiceImpl"/> <jaxws:server id="userService" serviceClass="com.wjl.UserService" address="/Users"> <jaxws:serviceBean> <ref bean="userServiceBean"/> </jaxws:serviceBean> </jaxws:server> </beans>
注意:
<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"/>一定要引入
现在启动tomcat 访问:http://localhost:8080/cxftest/Users?wsdl
若出现以下则表示服务端配置成功
下面是client端
1.不使用spring
首先在client中新建一个接口,和服务端的一样即可
在创建一个测试类如下
package client.wjl; import org.apache.cxf.jaxws.JaxWsProxyfactorybean; public class ClientClass { /** * @param args */ public static void main(String[] args) { //调用WebService JaxWsProxyfactorybean factory = new JaxWsProxyfactorybean(); factory.setServiceClass(UserService.class); factory.setAddress("http://localhost:8080/cxftest/Users"); UserService service = (UserService) factory.create(); System.out.println("#Client getUser"); UserBean user = service.getUser("wjl"); System.out.println(user); user.setAddress("dota"); service.setUser(user); } }
执行即可
2.使用spring
web,xml配置与服务端一样
spring配置为如下
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.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"/> <bean id="client" class="client.wjl.UserService" factory-bean="clientFactory" factory-method="create"/> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyfactorybean"> <property name="serviceClass" value="client.wjl.UserService"/> <property name="address" value="http://localhost:8080/cxftest/Users"/> </bean> </beans>
在创建测试类
package client.wjl; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClasspathXmlApplicationContext; public class clientSpring { /** * @param args */ public static void main(String[] args) { // Todo Auto-generated method stub ApplicationContext ctx = new ClasspathXmlApplicationContext( "applicationContext-server.xml"); UserService service = (UserService) ctx.getBean("client",UserService.class); UserBean user = service.getUser("reven"); System.out.println(user.getAddress()); System.out.println(user.getEmail()); user.setAddress("China"); service.setUser(user); } }执行即可
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。