|
该帖已经被评为良好帖
|
|
|---|---|
| 作者 | 正文 |
|
时间:2008-01-03 关键字: soa, web services, spring
关于Spring Web Services框架
Spring Web Services框架的特点
Spring Web Services框架的分析
1.为什么使用Contract First.
最佳实践认为:使用自顶向下的设计方式也就是采用XML/XSD to JAVA可以获得更多的益处,包括以下几点.
这样造成了Contract Last的问题:自底向上生成经常会得到无法重用的类型定义以及多个定义为表示语义等效信息的类型。相比而言: XML 模式规范定义范围比 Java 更广的用于描述消息结构的构造。其中包括各个选择、限制的派生、Annotation 及其他。因此,与采用其他方式相比,使用 WSDL 和 XSD 定义接口并生成框架 Java 代码的方式更好
比较二者,其实最大优劣的莫过于服务的变化性,Contract Last会让服务难于修改和快速变更,难于重用,用java开始设计,那么你最好保证你的服务是永久不改变的,或者事先你得反复审查你的服务接口。使其尽量保持不变性。
2.例子引入
Spring Web Servers提供了丰富的例子可供学习,下载其完整包可以在samples下面找到。这里也引用其中一个Echo sample介绍其开发过程。该例子实现web services client调用服务传送名字到服务器。然后服务提供者接收该名字,并附加信息返回给调用者。
定义你的XML <sch:EchoRequest xmlns:sch="http://www.upyaya.org/echo/schemas">
<sch:Echo>
<sch:Name>string</sch:Name>
</sch:Echo>
</sch:EchoRequest>使用XML Buddy转换成XSD.
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated from echo.xml by XMLBuddy -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.upyaya.org/echo/schemas"
xmlns="http://www.upyaya.org/echo/schemas"
elementFormDefault="qualified">
<xs:element name="Echo">
<xs:complexType>
<xs:sequence>
<xs:element ref="Name"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="EchoRequest">
<xs:complexType>
<xs:sequence>
<xs:element ref="Echo"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Name" type="xs:string"/>
</xs:schema>
当你熟悉了XSD的写法的时候,完全不用前面的XML开路。编辑一下XSD让其好看点,并未这个定义加上web services响应代码。结果如下: <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.upyaya.org/echo/schemas" elementFormDefault="qualified"
targetNamespace="http://www.upyaya.org/echo/schemas">
<xs:element name="EchoRequest">
<xs:complexType>
<xs:all>
<xs:element name="Echo" type="tns:EchoType"/>
</xs:all>
</xs:complexType>
</xs:element>
<xs:element name="EchoResponse">
<xs:complexType>
<xs:all>
<xs:element name="EchoResponse" type="tns:ReturnType"/>
</xs:all>
</xs:complexType>
</xs:element>
<xs:complexType name="EchoType">
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ReturnType">
<xs:sequence>
<xs:element name="Message" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>至此你的服务就完成。EchoRequest用于发送请求,EchoResponse用于响应。
定义后台服务接口。代码 package org.upyaya.sample.echo.service;
public interface EchoService {
public String echo(String name);
}
实现如下:为了简单,略去后台的调用。 package org.upyaya.sample.echo.service;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.upyaya.sample.echo.domain.dao.EchoDao;
public class EchoServiceImpl implements EchoService {
//private EchoDao echoDao;
public String echo(String name) {
if (name == null || name.trim().length() == 0) {
return "echo back: -please provide a name-";
}
SimpleDateFormat dtfmt = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss a");
return "echo back: name " + name + " received on "
+ dtfmt.format(Calendar.getInstance().getTime());
}
/* public EchoDao getEchoDao() {
return echoDao;
}
public void setEchoDao(EchoDao echoDao) {
this.echoDao = echoDao;
}*/
}
Spring的ApplicationContext就是Spring的魔法棒,指挥着组件间的合作。
1) 几个类的介绍:
2)Endpoint的实现,
endpoint是把传入消息处理后转为响应的类,通过继承AbstractMarshallingPayloadEndpoint重写invokeInternal方法来实现,invokeInternal 是这样的:protected Object invokeInternal(Object request) throws Exception,传入的是请求消息。返回的是响应消息。代码如下 package org.upyaya.sample.echo.endpoint;
import org.springframework.ws.server.endpoint.AbstractMarshallingPayloadEndpoint;
import org.upyaya.sample.echo.domain.schema.EchoRequest;
import org.upyaya.sample.echo.domain.schema.EchoResponse;
import org.upyaya.sample.echo.domain.schema.EchoType;
import org.upyaya.sample.echo.domain.schema.ReturnType;
import org.upyaya.sample.echo.service.EchoService;
public class EchoMasharlingEndpoint extends AbstractMarshallingPayloadEndpoint {
private EchoService echoService;
public EchoService getEchoService() {
return echoService;
}
public void setEchoService(EchoService echoService) {
this.echoService = echoService;
}
protected Object invokeInternal(Object request) throws Exception {
EchoRequest echoRequest = (EchoRequest)request;
EchoType echoType = echoRequest.getEcho();
System.out.println("-------here----------");
System.out.println(echoType.getName());
String returnMessage = echoService.echo(echoType.getName());
EchoResponse response = new EchoResponse();
ReturnType returnType = new ReturnType();
returnType.setMessage(returnMessage);
response.setEchoResponse(returnType);
return response;
}
}
注:本例采用的是Marshalling方式。因此需要使用JAX-B的API来对消息进行转换,JAX-B的eclipse插件可以轻松的实现XSD->JAVA.插件地址:https://jaxb-workshop.dev.java.net/
3.总结和附件 Spring Web services的优缺点。 1)优点方面
2)缺点方面
暂时做个总结吧
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
时间:2008-01-11
谢谢楼主的介绍!
修改一下刚才我的提问,刚才没有注意看 |
|
| 返回顶楼 | |
|
时间:2008-01-21
谢谢关注,我会不断更新的!最近主要忙于JCA,时间比较少,以后打算继续关注这个框架,也打算写写JCA的一些东西。
|
|
| 返回顶楼 | |
|
时间:2008-01-23
我不喜欢从xml到java,xml定义明显比java对象抽象得多,从java到xml能做出较好的面向对象设计,对参数和返回值的定义也更简单,我用的是XFire(XFire生成的Stub的Java代码比Axis的容易懂些)
|
|
| 返回顶楼 | |
|
时间:2008-01-23
还有,XFire能对Spring有良好的支持。。。安全方面方式也比较灵活
|
|
| 返回顶楼 | |
|
时间:2008-01-25
ECyzl2007 写道 还有,XFire能对Spring有良好的支持。。。安全方面方式也比较灵活
是的,目前XFire的确得到了很广泛的应用,有很多的best practice. Spring WS,刚成立不久,还需要磨练。 |
|
| 返回顶楼 | |
|
时间:2008-02-02
楼上两位, 注意 spring-ws 的设计宗旨是 contract first, 没有跟 xfire 比较的必要, 遇到异构系统时, 就能体会到 contract first 的好处
|
|
| 返回顶楼 | |
浏览 3678 次




