spring+webservice工程

服务器端

1.server-config.wsdd

<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
    <globalConfiguration>
        <parameter name="sendMultiRefs" value="true" />
        <parameter name="disablePrettyXML" value="true" />
        <parameter name="dotNetSoapEncFix" value="true" />
        <parameter name="enableNamespacePrefixOptimization" value="false" />
        <parameter name="sendXMLDeclaration" value="true" />
        <parameter name="sendXsiTypes" value="true" />
    </globalConfiguration>
    <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper" />
    <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder" />

    <service name="myServerInfo" provider="java:RPC">
        <parameter    name="allowedMethods"    value="*" />
        <parameter    name="className"  value="yourCompany.remote.webservice.JaxRpcXXXService" />
        <beanMapping  qname="shortName:List"       xmlns:shortName="urn:shortName" languageSpecificType="java:java.util.List" />
        <beanMapping  qname="shortName:ArrayList"  xmlns:shortName="urn:shortName" languageSpecificType="java:java.util.ArrayList" />
        <beanMapping  qname="shortName:OneBean" xmlns:scion="urn:shortName" languageSpecificType="java:yourBeanPath.OneBean" />
         <arrayMapping qname="shortName:List"       xmlns:shortName="urn:shortName" languageSpecificType="java:java.util.List" innerType="java:yourBeanPath.OneBean" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
    </service>

    <transport name="http">
        <requestFlow>
            <handler type="URLMapper" />
            <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler" />
        </requestFlow>
    </transport>

    <transport name="local">
        <responseFlow>
            <handler type="LocalResponder" />
        </responseFlow>
    </transport>

</deployment>

2.web.xml

<servlet>
        <servlet-name>axis</servlet-name>
        <servlet-class>
            org.apache.axis.transport.http.AxisServlet
        </servlet-class>
        <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>axis</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

3.代码层


import java.rmi.Remote;
public interface RemoteXXXService extends Remote {
    public  Object[] getXXX(String param);
}
----------------------------------------------------------------

import org.springframework.remoting.jaxrpc.ServletEndpointSupport;

import com.sanminasci.msportal.remote.RemoteServerInfoService;
import com.sanminasci.msportal.service.ManagerMuliMachineMap;

public class JaxRpcXXXService extends ServletEndpointSupport implements    RemoteXXXService {
    private XXXService xXX;
    protected void onInit() {
        xXX = (XXXService) getWebApplicationContext().getBean("xXX");
    }
    public  Object[] getXXX(String param) {
        return xXX.getXXX(param);
    }
}



客户端

1.spring配置文件(spring-application.xml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
        "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>WEB-INF/config.properties</value>
                <value>WEB-INF/log4j.properties</value>
            </list>
        </property>
    </bean>
 
    <bean id="myServerInfo" class="org.springframework.remoting.jaxrpc.JaxRpcPortProxyFactoryBean" lazy-init="true">
        <property name="serviceFactoryClass">
            <value>org.apache.axis.client.ServiceFactory</value>
        </property>
        <property name="wsdlDocumentUrl">
            <value>http://${serverName}:${httpPort}${contextPath}/services/myServerInfo?wsdl</value>
        </property>
        <property name="namespaceUri">
            <value>http://${serverName}:${httpPort}${contextPath}/services/myServerInfo</value>
        </property>
        <property name="serviceName">
            <value>JaxRpcXXXService</value>
        </property>
        <property name="portName">
            <value>myServerInfo</value>
        </property>
        <property name="serviceInterface">
            <value>service.MyService</value>
        </property>
        <property name="servicePostProcessors">
            <list>
                <bean class="util.BeanMappingProcessor" />   
            </list>
        </property>   
    </bean>        
</beans>

BeanMappingProcessor.java


import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.encoding.TypeMapping;
import javax.xml.rpc.encoding.TypeMappingRegistry;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import org.apache.log4j.Logger;
import org.springframework.remoting.jaxrpc.JaxRpcServicePostProcessor; 
public class BeanMappingProcessor implements    JaxRpcServicePostProcessor {     
    Logger logger = Logger.getLogger(this.getClass().getName()); 
    public static final String DEFAULT_ENCODING_STYLE_URI = "http://schemas.xmlsoap.org/soap/encoding/";  
    public static final String DEFAULT_TYPE_NAMESPACE_URI = "urn:shortName"; 
    private String encodingStyleUri = DEFAULT_ENCODING_STYLE_URI; 
    private String typeNamespaceUri = DEFAULT_TYPE_NAMESPACE_URI;
 
    public void setEncodingStyleUri(String encodingStyleUri) {
        this.encodingStyleUri = encodingStyleUri;
    }
      
    public void setTypeNamespaceUri(String typeNamespaceUri) {
        this.typeNamespaceUri = typeNamespaceUri;
    }

    public void postProcessJaxRpcService(Service service) {
        TypeMappingRegistry registry = service.getTypeMappingRegistry();
        TypeMapping mapping = registry.createTypeMapping();
        //core
        try{
        registerBeanMapping(mapping, List.class, "List");
        registerBeanMapping(mapping, ArrayList.class, "ArrayList");
        registerBeanMapping(mapping, OneBean.class, "OneBean");
        
        registry.register(this.encodingStyleUri, mapping);
        }catch(Exception e){
            logger.error("register:" + e.getMessage());
            e.printStackTrace();
        }
    }

    protected void registerBeanMapping(TypeMapping mapping, Class type,
            String name) {
        QName xmlType = new QName(this.typeNamespaceUri, name);
        mapping.register(type, xmlType,
                new BeanSerializerFactory(type, xmlType),
                new BeanDeserializerFactory(type, xmlType));
    }

}

你可能感兴趣的:(spring,xml,webservice)