spring+JAX-RPC(Axis) 构建webservice。

本文介绍了如何在Eclipse WTP环境下发布Web服务,并利用Spring的IOC管理JAX-RPC接口,实现与RMI接口的解耦。通过创建DAO和服务层,结合Spring的AOP和IOC,构建了一个非RMI接口的Web服务解决方案。
摘要由CSDN通过智能技术生成

介绍

关于用eclipse+wtp发布webservice是相当的简单。这里只简单介绍一下,wtp下发布webservice有以下几个步骤:

·创建Dynamic Web Project.(wtp自带项目)

·在soure folder 下面创建要发布的java bean。本例中发布了一个User.java

·在创建的工程上面新建Web Service. (wtp自带) 浏览中选择创建的User.java

·点击完成后,运行该工程,选择在服务器上运行,配置好服务器,本例发布用的Tomcat5.

发布完成,相当简单吧。

 

接下来讲webservice发布完成后,用springIOCwebservice进行管理。Spring使用JAX-RPC端口代理来暴露RMI或者非RMI的接口。

RMI接口:

好处:客户端不需要了解JAX-RPC

坏处:客户端需要对RMI对象有所了解。并且需要处理烦人的RemoteException.

RMI接口:

好处:客户端对RMIJAX-RPC都不需要了解,完全解耦。

坏处:水平有限,没有发现。

 

这里只讲解非RMI接口的实现情况:

 

『一』. 发布结果的回顾:

上面的webservice已经发布好了。这里列出被发布的User.java

User.java:

package  org.upyaya.webservice;

public   class  User  {

    
public String getAddress() {
        
return "Jingke Road,Zhangjiang,Shanghai";
    }


    
public String getUserName() {
        
return "Upyaya";
    }

}

发布完成后我们可以得到一个wsdl的URL。本例中是:

http://localhost:8080/SpringWebService/wsdl/User.wsdl

『二』. 解析WSDL

在eclipse+wtp的环境中解析wsdl也是很容易的。

·新建工程:使用 Dynamic Web Project 取名:SpringWebServiceClient。
·在工程 SpringWebServiceClient右键选择新建Web Service Client。
·输入上面的 wsdl URL.
·解析出需要的 java文件。

如下图:

『三』. 创建DAO layer 和 Service layer

DAO Layer:

IUserDAO.java

package  org.upyaya.webservice.dao;

public interface IUserDAO 
{
    
public
 String getAddress();

    
public
 String getUserName();
}

UserDAOImpl.java

package  org.upyaya.webservice.dao;

public class UserDAOImpl implements IUserDAO 
{
    
private
 IUserDAO userDAO;

    
public IUserDAO getUserDAO() 
{
        
return
 userDAO;
    }


    
public void setUserDAO(IUserDAO userDAO) {
        
this.userDAO =
 userDAO;
    }


    
public String getAddress() {
        
return
 userDAO.getAddress();
    }


    
public String getUserName() {
        
return
 userDAO.getUserName();
    }


}

Service Layer:

IUserService.java

package  org.upyaya.webservice.service;

public interface IUserService 
{
    
public
 String getAddress();

    
public
 String getUserName();
    
    
public
 String getUserInfo();
}

UserServiceImpl.java

package  org.upyaya.webservice.service;

import
 org.upyaya.webservice.dao.IUserDAO;

public class UserServiceImpl implements IUserService 
{

    
private
 IUserDAO userDAO;

    
public IUserDAO getUserDAO() 
{
        
return
 userDAO;
    }


    
public void setUserDAO(IUserDAO userDAO) {
        
this.userDAO =
 userDAO;
    }


    
public String getAddress() {
        
return
 userDAO.getAddress();
    }


    
public String getUserInfo() {
        
return getUserName() + "@" +
 getAddress();
    }


    
public String getUserName() {
        
return
 userDAO.getUserName();
    }


}

注意:看上面的几个累,有没有涉及到RMI和JAX-RPC?没有吧。完全的解耦,dao 和service完全不知到他们的存在

『四』. Spring AOP&IOC,解决webservice和DAO Layer之间的连接.

先看配置文件(Spring的家常便饭):

webservice.xml

 

<? xml version="1.0" encoding="UTF-8" ?>  
<! DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" 
  "http://www.springframework.org/dtd/spring-beans.dtd"
>  

< beans >   
    
    
< bean  id ="userDAO"  class ="org.upyaya.webservice.dao.UserDAOImpl" >
        
< property  name ="userDAO" >
            
< ref  local ="userServiceProxy" />
        
</ property >
    
</ bean >
    
    
< bean  id ="userService"  class ="org.upyaya.webservice.service.UserServiceImpl" >
        
< property  name ="userDAO"  ref ="userDAO"   />
    
</ bean >

<!--   Configure WebService bean -->

    
< bean  id ="userServiceProxy"  class ="org.springframework.remoting.jaxrpc.JaxRpcPortProxyFactoryBean" >
                   
<!--   Configure WebService wsdl url -->
           
< property  name ="wsdlDocumentUrl" >
                
< value > http://localhost:8080/SpringWebService/wsdl/User.wsdl </ value >
           
</ property >
                   
<!--   Configure WebService namespace uri  -->
           
< property  name ="namespaceUri" >
                
< value > http://webservice.upyaya.org </ value >
           
</ property >
                   
<!--   Configure WebService service name  -->
           
< property  name ="serviceName" >
                
< value > UserService </ value >
           
</ property >
                   
<!--   Configure WebService portName  -->
           
< property  name ="portName" >
                
< value > User </ value >
           
</ property >
                   
<!--  Configure WebService implements interface -->
           
< property  name ="serviceInterface" >
                
< value > org.upyaya.webservice.dao.IUserDAO </ value >
           
</ property >
           
< property  name ="portInterface" >
                   
< value > org.upyaya.webservice.User </ value >
           
</ property >
    
</ bean >

</ beans >
 
· wsdlDocumentUrl,这个不用解释了吧。
· namepaceUrl,wsdl服务的命名空间URL。
· servicename,wsdl的服务名称。
· portName,服务端口。
· serviceInterface,webservice实现的接口。是我们指定的业务接口
· portInterface,RMI服务接口。

 『四』. 测试

Client.java

package  org.upyaya.webservice.client;

import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.FileSystemXmlApplicationContext;
import  org.upyaya.webservice.service.IUserService;

public   class  UserClient  {

    
public static void main(String[] args) {
         ApplicationContext context 
= 
                
new FileSystemXmlApplicationContext(
                        
"webservice.xml");
         IUserService service 
= (IUserService)context.getBean("userService");
         System.out.println(service.getUserInfo());
    }


}

 
这里为了方便没有在web里面展现。但是展现机制不是这里的重点。所以选择控制台打印,结果如下:

Output:

log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory).
log4j:WARN Please initialize the log4j system properly.
Upyaya@Jingke Road,Zhangjiang,Shanghai

附:Wsdl文件

<? xml version="1.0" encoding="UTF-8" ?>
< wsdl:definitions  targetNamespace ="http://webservice.upyaya.org"  xmlns:impl ="http://webservice.upyaya.org"  xmlns:intf ="http://webservice.upyaya.org"  xmlns:apachesoap ="http://xml.apache.org/xml-soap"  xmlns:wsdlsoap ="http://schemas.xmlsoap.org/wsdl/soap/"  xmlns:xsd ="http://www.w3.org/2001/XMLSchema"  xmlns:wsdl ="http://schemas.xmlsoap.org/wsdl/" >
<!-- WSDL created by Apache Axis version: 1.3
Built on Oct 05, 2005 (05:23:37 EDT)
-->
 
< wsdl:types >
  
< schema  xmlns ="http://www.w3.org/2001/XMLSchema"  targetNamespace ="http://webservice.upyaya.org"  elementFormDefault ="qualified" >
   
< element  name ="getAddress" >
    
< complexType />
   
</ element >
   
< element  name ="getAddressResponse" >
    
< complexType >
     
< sequence >
      
< element  name ="getAddressReturn"  type ="xsd:string" />
     
</ sequence >
    
</ complexType >
   
</ element >
   
< element  name ="getUserName" >
    
< complexType />
   
</ element >
   
< element  name ="getUserNameResponse" >
    
< complexType >
     
< sequence >
      
< element  name ="getUserNameReturn"  type ="xsd:string" />
     
</ sequence >
    
</ complexType >
   
</ element >
  
</ schema >
 
</ wsdl:types >

   
< wsdl:message  name ="getUserNameResponse" >

      
< wsdl:part  name ="parameters"  element ="impl:getUserNameResponse" />

   
</ wsdl:message >

   
< wsdl:message  name ="getUserNameRequest" >

      
< wsdl:part  name ="parameters"  element ="impl:getUserName" />

   
</ wsdl:message >

   
< wsdl:message  name ="getAddressRequest" >

      
< wsdl:part  name ="parameters"  element ="impl:getAddress" />

   
</ wsdl:message >

   
< wsdl:message  name ="getAddressResponse" >

      
< wsdl:part  name ="parameters"  element ="impl:getAddressResponse" />

   
</ wsdl:message >

   
< wsdl:portType  name ="User" >

      
< wsdl:operation  name ="getAddress" >

         
< wsdl:input  name ="getAddressRequest"  message ="impl:getAddressRequest" />

         
< wsdl:output  name ="getAddressResponse"  message ="impl:getAddressResponse" />

      
</ wsdl:operation >

      
< wsdl:operation  name ="getUserName" >

         
< wsdl:input  name ="getUserNameRequest"  message ="impl:getUserNameRequest" />

         
< wsdl:output  name ="getUserNameResponse"  message ="impl:getUserNameResponse" />

      
</ wsdl:operation >

   
</ wsdl:portType >

   
< wsdl:binding  name ="UserSoapBinding"  type ="impl:User" >

      
< wsdlsoap:binding  style ="document"  transport ="http://schemas.xmlsoap.org/soap/http" />

      
< wsdl:operation  name ="getAddress" >

         
< wsdlsoap:operation  soapAction ="" />

         
< wsdl:input  name ="getAddressRequest" >

            
< wsdlsoap:body  use ="literal" />

         
</ wsdl:input >

         
< wsdl:output  name ="getAddressResponse" >

            
< wsdlsoap:body  use ="literal" />

         
</ wsdl:output >

      
</ wsdl:operation >

      
< wsdl:operation  name ="getUserName" >

         
< wsdlsoap:operation  soapAction ="" />

         
< wsdl:input  name ="getUserNameRequest" >

            
< wsdlsoap:body  use ="literal" />

         
</ wsdl:input >

         
< wsdl:output  name ="getUserNameResponse" >

            
< wsdlsoap:body  use ="literal" />

         
</ wsdl:output >

      
</ wsdl:operation >

   
</ wsdl:binding >

   
< wsdl:service  name ="UserService" >

      
< wsdl:port  name ="User"  binding ="impl:UserSoapBinding" >

         
< wsdlsoap:address  location ="http://localhost:8080/SpringWebService/services/User" />

      
</ wsdl:port >

   
</ wsdl:service >

</ wsdl:definitions >

 

《完》

META-INF/MANIFEST.MF META-INF/license.txt org.springframework.remoting.caucho.BurlapClientInterceptor.class org.springframework.remoting.caucho.BurlapProxyFactoryBean.class org.springframework.remoting.caucho.BurlapServiceExporter.class org.springframework.remoting.caucho.Hessian1SkeletonInvoker.class org.springframework.remoting.caucho.Hessian2SkeletonInvoker.class org.springframework.remoting.caucho.HessianClientInterceptor.class org.springframework.remoting.caucho.HessianProxyFactoryBean.class org.springframework.remoting.caucho.HessianServiceExporter.class org.springframework.remoting.caucho.HessianSkeletonInvoker.class org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor.class org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor.class org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration.class org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.class org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean.class org.springframework.remoting.httpinvoker.HttpInvokerRequestExecutor.class org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter.class org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor.class org.springframework.remoting.jaxrpc.JaxRpcPortClientInterceptor.class org.springframework.remoting.jaxrpc.JaxRpcPortProxyFactoryBean.class org.springframework.remoting.jaxrpc.JaxRpcServicePostProcessor.class org.springframework.remoting.jaxrpc.JaxRpcSoapFaultException.class org.springframework.remoting.jaxrpc.LocalJaxRpcServiceFactory.class org.springframework.remoting.jaxrpc.LocalJaxRpcServiceFactoryBean.class org.springframework.remoting.jaxrpc.ServletEndpointSupport.class org.springframework.remoting.jaxrpc.support.AxisBeanMappingServicePostProcessor.class org.springframework.remoting.jaxws.JaxWsPortClientInterceptor.class org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean.class org.springframework.remoting.jaxws.JaxWsSoapFaultException.class org.springframework.remoting.jaxws.LocalJaxWsServiceFactory.class org.springframework.remoting.jaxws.LocalJaxWsServiceFactoryBean.class org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter.class org.springframework.web.HttpRequestHandler.class org.springframework.web.HttpRequestMethodNotSupportedException.class org.springframework.web.HttpSessionRequiredException.class org.springframework.web.bind.EscapedErrors.class org.springframework.web.bind.MissingServletRequestParameterException.class org.springframework.web.bind.RequestUtils.class org.springframework.web.bind.ServletRequestBindingException.class org.springframework.web.bind.ServletRequestDataBinder.class org.springframework.web.bind.ServletRequestParameterPropertyValues.class org.springframework.web.bind.ServletRequestUtils.class org.springframework.web.bind.WebDataBinder.class org.springframework.web.bind.annotation.InitBinder.class org.springframework.web.bind.annotation.ModelAttribute.class org.springframework.web.bind.annotation.RequestMapping.class org.springframework.web.bind.annotation.RequestMethod.class org.springframework.web.bind.annotation.RequestParam.class org.springframework.web.bind.annotation.SessionAttributes.class org.springframework.web.bind.support.ConfigurableWebBindingInitializer.class org.springframework.web.bind.support.DefaultSessionAttributeStore.class org.springframework.web.bind.support.SessionAttributeStore.class org.springframework.web.bind.support.SessionStatus.class org.springframework.web.bind.support.SimpleSessionStatus.class org.springframework.web.bind.support.WebBindingInitializer.class org.springframework.web.context.ConfigurableWebApplicationContext.class org.springframework.web.context.ContextLoader.class org.springframework.web.context.ContextLoaderListener.class org.springframework.web.context.ContextLoaderServlet.class org.springframework.web.context.ServletConfigAware.class org.springframework.web.context.ServletContextAware.class org.springframework.web.context.WebApplicationContext.class org.springframework.web.context.request.AbstractRequestAttributes.class org.springframework.web.context.request.AbstractRequestAttributesScope.class org.springframework.web.context.request.Log4jNestedDiagnosticContextInterceptor.class org.springframework.web.context.request.RequestAttributes.class org.springframework.web.context.request.RequestContextHolder.class org.springframework.web.context.request.RequestContextListener.class org.springframework.web.context.request.RequestScope.class org.springframework.web.context.request.ServletRequestAttributes.class org.springframework.web.context.request.ServletWebRequest.class org.springframework.web.context.request.SessionScope.class org.springframework.web.context.request.WebRequest.class org.springframework.web.context.request.WebRequestInterceptor.class org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.class org.springframework.web.context.support.ContextExposingHttpServletRequest.class org.springframework.web.context.support.GenericWebApplicationContext.class org.springframework.web.context.support.HttpRequestHandlerServlet.class org.springframework.web.context.support.PerformanceMonitorListener.class org.springframework.web.context.support.RequestHandledEvent.class org.springframework.web.context.support.ServletContextAttributeExporter.class org.springframework.web.context.support.ServletContextAttributeFactoryBean.class org.springframework.web.context.support.ServletContextAwareProcessor.class org.springframework.web.context.support.ServletContextFactoryBean.class org.springframework.web.context.support.ServletContextParameterFactoryBean.class org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer.class org.springframework.web.context.support.ServletContextResource.class org.springframework.web.context.support.ServletContextResourceLoader.class org.springframework.web.context.support.ServletContextResourcePatternResolver.class org.springframework.web.context.support.ServletRequestHandledEvent.class org.springframework.web.context.support.StaticWebApplicationContext.class org.springframework.web.context.support.WebApplicationContextUtils.class org.springframework.web.context.support.WebApplicationObjectSupport.class org.springframework.web.context.support.XmlWebApplicationContext.class org.springframework.web.filter.AbstractRequestLoggingFilter.class org.springframework.web.filter.CharacterEncodingFilter.class org.springframework.web.filter.CommonsRequestLoggingFilter.class org.springframework.web.filter.DelegatingFilterProxy.class org.springframework.web.filter.GenericFilterBean.class org.springframework.web.filter.Log4jNestedDiagnosticContextFilter.class org.springframework.web.filter.OncePerRequestFilter.class org.springframework.web.filter.RequestContextFilter.class org.springframework.web.filter.ServletContextRequestLoggingFilter.class org.springframework.web.jsf.DecoratingNavigationHandler.class org.springframework.web.jsf.DelegatingNavigationHandlerProxy.class org.springframework.web.jsf.DelegatingPhaseListenerMulticaster.class org.springframework.web.jsf.DelegatingVariableResolver.class org.springframework.web.jsf.FacesContextUtils.class org.springframework.web.jsf.SpringBeanVariableResolver.class org.springframework.web.jsf.WebApplicationContextVariableResolver.class org.springframework.web.jsf.el.SpringBeanFacesELResolver.class org.springframework.web.jsf.el.WebApplicationContextFacesELResolver.class org.springframework.web.multipart.MaxUploadSizeExceededException.class org.springframework.web.multipart.MultipartException.class org.springframework.web.multipart.MultipartFile.class org.springframework.web.multipart.MultipartHttpServletRequest.class org.springframework.web.multipart.MultipartResolver.class org.springframework.web.multipart.commons.CommonsFileUploadSupport.class org.springframework.web.multipart.commons.CommonsMultipartFile.class org.springframework.web.multipart.commons.CommonsMultipartResolver.class org.springframework.web.multipart.support.AbstractMultipartHttpServletRequest.class org.springframework.web.multipart.support.ByteArrayMultipartFileEditor.class org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest.class org.springframework.web.multipart.support.MultipartFilter.class org.springframework.web.multipart.support.StringMultipartFileEditor.class org.springframework.web.util.CookieGenerator.class org.springframework.web.util.ExpressionEvaluationUtils.class org.springframework.web.util.HtmlCharacterEntityDecoder.class org.springframework.web.util.HtmlCharacterEntityReferences.class org.springframework.web.util.HtmlUtils.class org.springframework.web.util.HttpSessionMutexListener.class org.springframework.web.util.IntrospectorCleanupListener.class org.springframework.web.util.JavaScriptUtils.class org.springframework.web.util.Log4jConfigListener.class org.springframework.web.util.Log4jConfigServlet.class org.springframework.web.util.Log4jWebConfigurer.class org.springframework.web.util.NestedServletException.class org.springframework.web.util.TagUtils.class org.springframework.web.util.UrlPathHelper.class org.springframework.web.util.WebAppRootListener.class org.springframework.web.util.WebUtils.class org/springframework/web/context/ContextLoader.properties org/springframework/web/util/HtmlCharacterEntityReferences.properties
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值