1.增加applicationContext-webservoce.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"/> <!-- 注意下面的address,这里的address的名称就是访问的WebService的name --> <jaxws:endpoint id="helloWorld" implementor="com.jetsum.webservice.impl.HelloWorldImpl" address="/helloWorld" /> <jaxws:endpoint id="helloworld6" implementor="com.jetsum.webservice.impl.Helloworld1Impl" address="/helloworld2" /> </beans> 2.applicationContext.xml文件中增加资源的导入 <import resource="applicationContext-WebService.xml" /> 3. 在web.xml 文件中增加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>/webservice/*</url-pattern> </servlet-mapping> 4.创建java bean类 HelloWorld.java(接口类) package com.jetsum.webservice; import java.util.List; import javax.jws.WebService; import javax.jws.WebP@R_502_6460@m; import com.jetsum.zhxy.entity.StuStudent; /** * * @author Daley * 调用webservice服务及接口方法 * */ @WebService public interface HelloWorld { String sayHi(@WebP@R_502_6460@m(name="text")String text); String sayHiToUser(StuStudent user); String[] SayHiToUserList(List<StuStudent> userList); } HelloWorldImpl.java(实现类) package com.jetsum.webservice.impl; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import javax.jws.WebService; import com.jetsum.webservice.HelloWorld; import com.jetsum.zhxy.entity.StuStudent; /** * * @author Daley * 对应webservice服务及接口的实现方法 * */ @WebService(endpointInterface="com.jetsum.webservice.HelloWorld",serviceName="HelloWorld") public class HelloWorldImpl implements HelloWorld { Map<Integer,StuStudent> users = new LinkedHashMap<Integer,StuStudent>(); public String sayHi(String text) { return "Hello " + text; } public String sayHiToUser(StuStudent user) { users.put(users.size()+1,user); return "Hello "+ user.getName(); } public String[] SayHiToUserList(List<StuStudent> userList) { String[] result = new String[userList.size()]; int i=0; for(StuStudent u:userList){ result[i] = "Hello " + u.getName(); i++; } return result; } } WebserviceCenter.java(测试类) package com.jetsum.webservice; import org.apache.cxf.jaxws.JaxWsProxyfactorybean; import com.jetsum.zhxy.entity.StuStudent; public class WebserviceCenter { public static <T> T getService(Class<T> tag){ //创建调用客户端生成Factory用于调用Webserivce JaxWsProxyfactorybean svr = new JaxWsProxyfactorybean(); //设置的webservice服务端接口 svr.setServiceClass(tag); //设置调用地址 svr.setAddress(getUrlByClassname(tag)); return (T)svr.create(); } /** * @p@R_502_6460@m args */ public static void main(String[] args) { HelloWorld hw = WebserviceCenter.getService(HelloWorld.class); System.out.println(hw.sayHi("fda")); StuStudent s=new StuStudent(); s.setName("linda"); System.out.println(hw.sayHiToUser(s)); } public static String getUrlByClassname(Class c){ //运营平台客户端调用地址 //helloworld例子 if(c==HelloWorld.class) return "http://localhost:6033/zhxy/webservice/helloWorld?wsdl"; //学籍系统 return null; } } ************************************************* 以上是server服务端通过Spring整合配置的,下面我们将Client端也通过Spring配置完成整合 首先增加applicationContext-client.xml配置文件。 <?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"/> <jaxws:client id="userWsClient" serviceClass="com.hoo.service.IComplexUserService" address="http://localhost:8080/webservice/helloWorld"/> </beans> 客户端代码 package com.hoo.client; import org.apache.cxf.jaxws.JaxWsProxyfactorybean; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClasspathXmlApplicationContext; import com.hoo.entity.User; import com.hoo.service.IComplexUserService; /** * <b>function:</b>请求Spring整合CXF的WebService客户端 * @author hoojo * @createDate 2011-3-28 下午03:20:35 * @file SpringUsersWsClient.java * @package com.hoo.client * @project CXFWebService * @blog http://blog.csdn.net/IBM_hoojo * @email [email protected] * @version 1.0 */ public class SpringUsersWsClient { public static void main(String[] args) { ApplicationContext ctx = new ClasspathXmlApplicationContext("applicationContext-client.xml"); IComplexUserService service = ctx.getBean("userWsClient",IComplexUserService.class); System.out.println("#############Client getUserByName##############"); User user = service.getUserByName("hoojo"); System.out.println(user); user.setAddress("China-Guangzhou"); service.setUser(user); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。