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 孙效宁
- *
- */
- @WebService
- public 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>
- <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;
- }
- @Override
- public CustomVO getCustomVO(String id) {
- System.err.println("id:"+id);
- CustomVO customVO = new CustomVO();
- customVO.setAge(22);
- customVO.setBirthDay(new Date());
- return customVO;
- }
- @Override
- public 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;
- }
- @Override
- public 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;
- }
- @Override
- public void save(CustomVO customVO) {
- System.err.println("age:"+customVO.getAge()+"birthday:"+customVO.getBirthDay());
- }
- @Override
- public void saveCustomList(List<CustomVO> customVOList) {
- System.err.println("size:"+customVOList.size());
- }
- @Override
- public void saveCustomMap(Map<String, CustomVO> customVOMap) {
- System.err.println("map size:"+customVOMap.size());
- }
- </span><p><span style="font-size:18px;">}</span></p>
- <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.xsd
- http://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>