springmvc 集成apache cxf 开发webservice 示例
来源:互联网 发布:阿里云 dns服务器地址 编辑:程序博客网 时间:2024/05/24 04:46
1.添加apache cxf 类库
<span style="font-size:18px;"><!-- cxf webservice --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.3</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.3</version></span><p><span style="font-size:18px;"></dependency></span></p>
2.开发webservice接口
<span style="font-size:18px;">package com.sky.springmvc.webservice;import java.util.List;import java.util.Map;import javax.jws.WebParam;import javax.jws.WebService;import com.sky.springmvc.vo.CustomVO;/** * 服务端接口 * @author 孙效宁 * */@WebServicepublic interface HelloWorld { String sayHi(@WebParam(name="text") String text); CustomVO getCustomVO(@WebParam(name="id") String id); List<CustomVO> getCustomVOList(); Map<String,CustomVO> getCutomMap(); void save(@WebParam(name="customVO") CustomVO customVo); void saveCustomList(@WebParam(name="customVOList") List<CustomVO> customVOList); void saveCustomMap(@WebParam(name="customVOMap") Map<String,CustomVO> customVOMap);</span><p><span style="font-size:18px;">}</span></p>3.开发webservic接口实现类
<span style="font-size:18px;">package com.sky.springmvc.webservice;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.jws.WebService;import com.sky.springmvc.vo.CustomVO;/** * 服务端实现类 * @author 孙效宁 * */@WebService(endpointInterface = "com.sky.springmvc.webservice.HelloWorld",serviceName="HelloWorld")public class HelloWorldImpl implements HelloWorld { public String sayHi(String text) { System.out.println("sayHi called"); return "Hello " + text; }@Overridepublic CustomVO getCustomVO(String id) {System.err.println("id:"+id);CustomVO customVO = new CustomVO();customVO.setAge(22);customVO.setBirthDay(new Date());return customVO;}@Overridepublic List<CustomVO> getCustomVOList() {CustomVO customVO = new CustomVO();customVO.setAge(22);customVO.setBirthDay(new Date());List<CustomVO> customVOList = new ArrayList<CustomVO>();customVOList.add(customVO);return customVOList;}@Overridepublic Map<String, CustomVO> getCutomMap() {CustomVO customVO = new CustomVO();customVO.setAge(22);customVO.setBirthDay(new Date());Map<String,CustomVO> customMap = new HashMap<String,CustomVO>();customMap.put("1", customVO);return customMap;}@Overridepublic void save(CustomVO customVO) {System.err.println("age:"+customVO.getAge()+"birthday:"+customVO.getBirthDay());}@Overridepublic void saveCustomList(List<CustomVO> customVOList) {System.err.println("size:"+customVOList.size());}@Overridepublic void saveCustomMap(Map<String, CustomVO> customVOMap) {System.err.println("map size:"+customVOMap.size());}</span><p><span style="font-size:18px;">}</span></p>3.配置公开的服务
<span style="font-size:18px;"><?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"/> <jaxws:endpoint id="helloWorld" implementor="com.sky.springmvc.webservice.HelloWorldImpl" address="/helloWorld"/></beans></span><span style="font-size:24px;"></span>
4.客户端调用webservice 服务示例
方案一:
package com.sky.springmvc.webservice;import org.apache.cxf.interceptor.LoggingInInterceptor;import org.apache.cxf.interceptor.LoggingOutInterceptor;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.sky.springmvc.vo.CustomVO;/** * webservice 调用示例 * @author 孙效宁 * */public class Client { /** * 使用配置文件形式,调用webserve 服务 */ @Test public void test1() {ApplicationContext app = new ClassPathXmlApplicationContext("config/spring/cxf-client.xml");HelloWorld hello = (HelloWorld) app.getBean("helloClient");hello.sayHi("hh"); } /** * 使用apache cxf api调用webservice 服务 */ @Test public void test2(){ JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.getInInterceptors().add(new LoggingInInterceptor()); factory.getOutInterceptors().add(new LoggingOutInterceptor()); factory.setServiceClass(HelloWorld.class); factory.setAddress("http://localhost:8080/springmvc/webservices/helloWorld"); HelloWorld client = (HelloWorld) factory.create(); String reply = client.sayHi("HI"); System.out.println("Server said: " + reply); CustomVO customVO = client.getCustomVO("3"); System.err.println("customVO:"+customVO); }<p>}</p><p><pre name="code" class="html"><!-- 客户端源码配置 --><?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" 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.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <jaxws:client id="helloClient" serviceClass="com.sky.springmvc.webservice.HelloWorld" address="http://localhost:8080/springmvc/webservices/helloWorld" /></beans>方案1的问题:如何知道服务端的接口类型以及接口参数使用类型,接口返回参数类型?
方案2::使用wsimport 工具生成客户端源码然后直接利用生成的客户端源码调用webservice服务,方案2不存在方案1存在的问题
1.生成客户端代码:
wsimport -s . -p com.sky.springmvc.wsutil.helloworld http://localhost:8080/springmvc/webservices/helloWorld?wsdl
2.利用客户端代码调用webservice 服务
package com.sky.springmvc;import java.util.ArrayList;import java.util.Date;import java.util.List;import org.junit.Test;import com.sky.springmvc.wsutil.helloWorld.CustomVO;import com.sky.springmvc.wsutil.helloWorld.HelloWorld;import com.sky.springmvc.wsutil.helloWorld.HelloWorld_Service;/** * 利用wsimport 工具生成的客户端调用webservice服务 * @author 孙效宁 * */public class ClientDemo { @Test public void test1(){HelloWorld helloWorld = new HelloWorld_Service().getHelloWorldImplPort();System.err.println("age:"+helloWorld.getCustomVO("3").getAge());System.err.println("list:"+helloWorld.getCustomVOList());System.err.println("map:"+helloWorld.getCutomMap());CustomVO customVO = new CustomVO();customVO.setAge(22);customVO.setBirthDay(new DateTest().convertToXMLGregorianCalendar(new Date()));helloWorld.save(customVO);List<CustomVO> customVOList = new ArrayList<CustomVO>();customVOList.add(customVO);helloWorld.saveCustomList(customVOList); }}最重要的配置文件web.xml文件差点儿给忘了
<!-- cxf webservice --> <servlet> <description>Apache CXF Endpoint</description> <display-name>cxf</display-name> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/webservices/*</url-pattern> </servlet-mapping>
0 0
- springmvc 集成apache cxf 开发webservice 示例
- springmvc 集成apache cxf 开发webservice 示例
- SpringMVC整合CXF开发WebService
- Apache CXF实现WebService开发(一)
- Apache CXF实现WebService开发(二)
- Apache-CXF开发Webservice学习笔记
- WebService-04-CXF与Spring集成开发WebService服务
- 使用cxf在springMVC环境搭建webservice示例-服务端
- 使用cxf在springMVC环境搭建webservice示例--客户端
- CXF webService集成spring
- SpringMVC添加CXF webservice
- springmvc CXF webservice
- cxf-webservice完整示例
- cxf与springmvc集成
- 构建基于CXF的WebService服务(4)--CXF与SpringMVC集成
- Apache CXF WebService 使用
- Apache CXF WebService 使用
- apache cxf解析webservice
- java className失败的原因
- Ubuntu下如何找回 /var/log/messages
- 初学Kruskal——Prime
- iOS 使用MJRefresh刷新
- java基础
- springmvc 集成apache cxf 开发webservice 示例
- Servlet
- 【计蒜客】难题题库 003 判断质数
- 离队后的杂念
- OC语言-NSMutableArray为什么要用strong来修饰
- 212 Word Search II [Leetcode]
- python内建函数——类型转化函数
- 几种常见模式识别算法整理和总结
- hdu 2243(ac自动机+矩阵快速幂)