Spring 实现远程访问详解——webservice

前几章分别介绍了spring rmi,spring httpinvoker,httpclient实现远程访问。本章将通过webserver技术实现spring远程访问。

Spring Web Services 是基于 Spring 框架的 Web 服务框架,主要侧重于基于文档驱动的Web服务,提供 SOAP 服务开发,允许通过多种方式创建 Web 服务。本章利用Apache CXF构建和开发webservice.

 

1.      webservice远程访问流程

1)      pom文件引入cxf和wsdl相关包依赖

2)      服务端创建webservice接口

3)      服务端实现webservice接口

4)      服务端配置暴露webservice接口

5)      客户端创建暴露的webservice接口

6)      客户端调用webservice接口

2.      Webservice具体实现

1)      pom文件引入cxf和wsdl相关包依赖

3.1.6
 
           org.apache.cxf
           cxf-core
           ${cxf.version}
 
       
       
           org.apache.cxf
           cxf-rt-bindings-soap
           ${cxf.version}
       
 
       
           org.apache.cxf
           cxf-rt-databinding-jaxb
           ${cxf.version}
       
 
       
           org.apache.cxf
           cxf-rt-frontend-simple
           ${cxf.version}
       
 
       
           org.apache.cxf
           cxf-rt-transports-http
           ${cxf.version}
       
       
           org.apache.cxf
           cxf-rt-frontend-jaxws
           ${cxf.version}
       
       
           org.apache.cxf
           cxf-rt-wsdl
           ${cxf.version}
       
       
           wsdl4j
           wsdl4j
           1.6.3
     

2)      服务端创建webservice接口

package com.lm.core.service;
 
importjava.util.List;
 
importjavax.jws.WebParam;
importjavax.jws.WebService;
 
importcom.lm.core.entity.User;
 
@WebService
publicinterface UserWsService {
   ListgetUserByAcount(@WebParam(name="name") Stringname,@WebParam(name="password") String password);
  
   void insert(User user);
}

3)      服务端实现webservice接口

package com.lm.core.service.impl;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.jws.WebService;
 
import org.springframework.beans.factory.annotation.Autowired;
 
import com.lm.core.entity.User;
import com.lm.core.mapper.UserMapper;
import com.lm.core.service.UserWsService;
 
@WebService
public class UserWsServiceImpl implements UserWsService {
 
    @Autowired
    private UserMapper userMapper;
    @Override
    public List getUserByAcount(String name, String password) {
       System.err.println("获取用户信息:" + name + password);
       return new ArrayList();
    }
    @Override
    public void insert(User user) {
       System.err.println("开始插入用户信息:" + user.toString());
    }
 
}

4)      服务端配置暴露webservice接口

配置头信息:


 
 
 
 
    
    
        
            
        

5)      服务端web.xml配置


        CXFService
        org.apache.cxf.transport.servlet.CXFServlet
    
    
        CXFService
        /webservice/*

6)      服务端运行显示

7)      客户端创建暴露的webservice接口

package com.lm.core.service;
 
import java.util.List;
 
import javax.jws.WebParam;
import javax.jws.WebService;
 
import com.lm.core.entity.User;
 
@WebService
public interface UserWsService {
    List getUserByAcount(@WebParam(name="name")String name,@WebParam(name="password")String password);
   
    void insert(User user);
}

8)      客户端配置服务器暴露接口

 
   
     

9)      客户端调用webservice接口

@RequestMapping(value = "/wsTest")
    @ResponseBody
    public BaseMapVo wsTest(String name, String password) {
       BaseMapVo vo = new BaseMapVo();
       long startDate = Calendar.getInstance().getTimeInMillis();
       System.out.println("wsTest客户端开始调用" + startDate);
       UserWsService ws = (UserWsService) ApplicationContextUtil.getInstance().getBean("userWsService");
       ws.getUserByAcount("张三", ":张三的密码");
       System.out.println("wsTest客户端调用结束" +  (Calendar.getInstance().getTimeInMillis()-startDate));
       vo.setRslt("sucess");
        return vo;
}

3.      注意异常问题

1)      服务端和客户端接口和实现需要加入@WebService注解

否则:Exception in thread "main"javax.xml.ws.WebServiceException: Could not find wsdl:binding operation infofor web method

原因是因为客户端的service中的方法没有跟服务端绑定,只需要将客户端的Service接口加上注解@WebService,异常便解决了

2)      暴露接口中参数最好加上@WebParam,实现参数命名

否则会导致参数名称不匹配现象。


代码路径:http://download.csdn.net/detail/a123demi/9495931

你可能感兴趣的:(Spring,远程访问)