webservices相关配置-cxf
发布时间:2020-12-16 23:20:54 所属栏目:安全 来源:网络整理
导读: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/
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.WebParam; import com.jetsum.zhxy.entity.StuStudent; /** *? * @author Daley * 调用webservice服务及接口方法 * */ @WebService public interface HelloWorld { ? ? String sayHi(@WebParam(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(); } /** * @param 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 hoojo_@126.com ?* @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); ? ? } }
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- vim – 选择包含破折号的整个单词
- IT部门面向云环境进行迁移时常见的三类错误
- bootstrap fileinput 组件整合SpringMVC上传图片到本地磁盘
- ionic/angular $http post form-data请求
- “模块化”Scala指南
- Angular 2路由器在Resolve中使用BehaviorSubject Observabl
- bootstrap-datetimepicker 的使用
- angularjs – 如何使用Angular ui-router设置默认状态
- 角 – 如何使用@ ngrx / store获取状态对象的当前值?
- 是否有适用于Vim的高级(例如bigram)自动完成插件?