Spring WS

spring-ws服务端和简单的eclipse生成的webservice 客户端 示例

文章分类:Java编程

spring -ws是基于契约先行(contract first)的原则实现的web service。

自从JDK1.5的annotation出现以后,我偏向于写注解而不写xml,所以我在项目中尽量写注解来替代xml

闲话少说。动手做。

必备包:spring.jar ; spring-ws-1.5.8-all.jar ; wsdl4j-1.6.1.jar ; spring-mvc-2.5.6.jar;

XmlSchema-1.4.3.jar ; commons-logging-1.1.1.jar

spring-ws是契约先行,先写wsdl文件的内容,然后生成所需要的java类。

而wsdl是由包含请求和返回以及数据xsd文件,使用spring-ws生成的。

这里演示一个简单的spring-ws

一个人客户端输入自己的卡号,返回这个人的所有信息。

xsd文件(workerInfo.xsd)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="
http://www.w3.org/2001/XMLSchema "
           xmlns:tns="
http://ajjws.com/schemas/workerinfo "
           elementFormDefault="qualified"
           targetNamespace="
http://ajjws.com/schemas/workerinfo ">

     <!--查询请求,输入的是一个字符串的证件号码CardNo-->
    <xs:element name="WorkerRequest">
    <xs:complexType>
       <xs:all>
        <xs:element name="CardNo" type="xs:string" />
       </xs:all>
    </xs:complexType>
    </xs:element>    

      <!--数据返回,返回的是一个WorkerInfo数据类型-->  
     <xs:element name="WorkerResponse">
     <xs:complexType>
        <xs:all>
         <xs:element name="WorkerResponse" type="tns:WorkerInfo" />
        </xs:all>
     </xs:complexType>
     </xs:element>     
      

<!--WorkerInfo数据类型其实就是一个Java类,由下面几个属性组成-->  
     <xs:complexType name="WorkerInfo">
     <xs:sequence>
        <xs:element name="id" type="xs:long"></xs:element>
        <xs:element name="cardNo" type="xs:string"></xs:element>
        <xs:element name="workerName" type="xs:string"></xs:element>
        <xs:element name="workerSex" type="xs:string"></xs:element>
     </xs:sequence>
     </xs:complexType>
</xs:schema>

对于spring-ws要配置的地方有

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance " xmlns=" http://java.sun.com/xml/ns/javaee " xmlns:web=" http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd " xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd " id="WebApp_ID" version="2.5">
<display-name>ajjws</display-name>
<servlet>
        <servlet-name>ajjws</servlet-name>
        <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
        <init-param>
            <param-name>transformWsdlLocations</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>ajjws</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

ajjws-servlet.xml:(名称和web.xml的servlet-name相同)

<?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:context="
http://www.springframework.org/schema/context "
       xsi:schemaLocation="
http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd ">

<!--对com.ajjws下的包里的类自动扫描,从而不用定义文件中bean,在类中使用@Service@component等-调用时候使用@Resource或者@AutoWired->    

<context:annotation-config/>
<context:component-scan base-package="com.ajjws"/>
   <!--指定xsd文件的地址-->
<bean id="schemaCollection" class="org.springframework.xml.xsd.commons.CommonsXsdSchemaCollection">

        <property name="xsds">
        <list>
           <value>/WEB-INF/xsds/workerInfo.xsd</value>        
            </list>
        </property>
        <property name="inline" value="true"/>
    </bean>
   
   

<bean class="org.springframework.ws.server.endpoint.adapter.GenericMarshallingMethodEndpointAdapter">
   <constructor-arg ref="marshaller" />
</bean>

<!--利用Jaxb2实现xml到Java的转换-->

<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
   <property name="contextPath" value="com.ajjws.beans" />
</bean>

<!--申明我们用注解来实现请求和Endpoint的映射对应-->

<bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping"/>
  

<!--通过xsd文件定义wsdl-->
<bean id="ajjws" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
    <property name="schemaCollection" ref="schemaCollection" />
        <property name="portTypeName" value="Ajj" />
    <property name="locationUri" value="
http://localhost:8080/ajjws/services " />
      <property name="targetNamespace" value="
http://ajjws.com/definitions " />
   </bean>

</beans>

安装jaxb eclipse插件,在xsd文件上点右键,选择jaxb2.1->run xjc

Spring WS

运行后会在com.ajjws.bean包下通过xsd文件生成的java类

如下所示:

Spring WS

Endpoint:(AjjwsEndpoint.java 响应web service的请求的文件)
package com.ajjws.ws;
import javax.annotation.Resource;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import com.ajjws.beans.ObjectFactory;
import com.ajjws.beans.WorkerInfo;
import com.ajjws.beans.WorkerRequest;
import com.ajjws.beans.WorkerResponse;
import com.ajjws.service.AjjService;
/**
* @author wyf
*
*/
@Endpoint
public class AjjwsEndpoint {

@Resource
private AjjService ajjService;//需配置自动扫描bean

@Resource
private ObjectFactory objectFactory;

@PayloadRoot(localPart="WorkerRequest",namespace="
http://ajjws.com/schemas/workerinfo ")
public WorkerResponse getWorkInfo(WorkerRequest request) {  
   WorkerResponse response=objectFactory.createWorkerResponse();
   WorkerInfo workerInfo=ajjService.getWorkInfo(request.getCardNo());
   response.setWorkerResponse(workerInfo);
   return response;
}
}

AjjService.java:

package com.ajjws.service;
import com.ajjws.beans.WorkerInfo;
public interface AjjService {
/**
* 获取证件信息
*/
public WorkerInfo getWorkInfo(String cardNo);
}
AjjServiceImpl.java

package com.ajjws.service;
import org.springframework.stereotype.Service;
import com.ajjws.beans.WorkerInfo;
/**
* @author wyf
*
*/
@Service("ajjService")
public class AjjServiceImpl implements AjjService {
public WorkerInfo getWorkInfo(String cardNo) {
   //此处仅为演示,为加入数据库
   System.out.println(cardNo);
   WorkerInfo worker=new WorkerInfo();
   worker.setCardNo("1234");
   worker.setId(1);
   worker.setWorkerName("wyf");
   worker.setWorkerSex("男");
   return worker;
}

}

 

客户端使用eclipse本身自带的生成web service client

Spring WS

Spring WS

点击finish,通过wsdl生成客户端所需要的Java类

结构如下

Spring WS
客户端调用代码:

package com.ajjSearch.web;
import java.rmi.RemoteException;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;

import com.ajjws.definitions.AjjProxy;
import com.ajjws.schemas.workerinfo.WorkerInfo;
import com.ajjws.schemas.workerinfo.WorkerRequest;
import com.ajjws.schemas.workerinfo.WorkerResponse;

public class WsClient extends WebServiceGatewaySupport{
public static void main(String[] args) throws RemoteException {
   doSomething();
}

public static void doSomething() throws RemoteException{
   WorkerRequest wreq=new WorkerRequest("1111");
   WorkerResponse wres=new AjjProxy().getAjj().worker(wreq);
   System.out.println(wres.getWorkerResponse());
   WorkerInfo wi=wres.getWorkerResponse();
   System.out.println(wi.getCardNo()+" "+wi.getId()+" "+wi.getWorkerName()+" "+wi.getWorkerSex());
}
}

你可能感兴趣的:(spring,Web,bean,servlet,javaee)