Web服务与SOA(二)

转载自:http://fengshen-xia.iteye.com/blog/293854

翻译自"Service Oriented Architecture with Java"(使用Java开发面向服务的架构)一书之第二章

[接上篇Web服务和SOA(一)] 现在,我们来看看如何使用 Java 实现 findById 这个 SOA 服务。我们将使用 JAXB 库来实现 XML 的自动绑定, JAXB 库已经包含在最新的 JDK6 中。因此,如果您使用的是 JDK6 ,您不需要下载额外的 jar 包,否则,您需要下载 JAXB ,并把它显式地加到您的服务器和客户端的类路径中。但是,因为下面的代码使用了 Java 注解功能 (Annotations) ,所以您需要 JDK5 或者更高版本编译和执行下面的代码。我们采用 Tomcat5.5 实现服务器端的 SOA 服务,实际上,我们只用了一个简单的 Servlet 来实现这个服务。

我们就动手写服务器和客户端的 Java 类来实现客户和服务器端的交互,这些类包括 Item ItemAction ItemActionResponse 。它们都是带有 Java 注解的 POJO 对象 (Plain Oil Java Objects) Java 注解在 XML 序列化和反序列化的过程中起了很重要的作用,示例代码如下所示:

代码清单 3 – XML 和注解进行绑定

 

  1. @XmlRootElement (name= "Item" )
  2. public   class  Item { 
  3. private   int  id; 
  4. private  String code; 
  5. private  String description; 
  6. ... getter/setter methods omitted ...
  7. @XmlRootElement (name= "ItemAction" )
  8. public   class  ItemAction{ 
  9. private  String method; 
  10. private  Item item; ...
  11. @XmlRootElement (name= "ItemActionResponse" )
  12. public   class  ItemActionResponse { 
  13. private  String retCode;
  14. private  Item item; ...

下面的代码是服务实现的主要部分,所有实现代码都包含在

Servlet doPost() 方法中,该 Servlet Web.xml 中的 URL 映射名为 itemCrudService

程序清单 4—itemCrudService 服务的服务器端实现

 

 

  1. protected   void  doPost(HttpServletRequest request, HttpServletResponse response) 
  2. throws  ServletException, IOException
  3. try
  4. JAXBContext jaxbContext = JAXBContext.newInstance (ItemAction. class , ItemActionResponse. class ); 
  5. Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); 
  6. //Receiving the XML request and transform it into a Java object 
  7. ItemAction itemAction = (ItemAction) 
  8. unmarshaller.unmarshal(request.getInputStream()); 
  9. //Do some action depending on the request content 
  10. String method = itemAction.getMethod(); 
  11. //Prepare the response as a Java object 
  12. ItemActionResponse itemActionResponse =  new  ItemActionResponse(); 
  13. if  ( "findById" .equals(method)){ 
  14. int  id = itemAction.getItem().getId(); 
  15. //Retrieve item (e.g. from db) 
  16. Item item =  new  Item(); 
  17. item.setId(id); 
  18. item.setCode( "Item XYZ" ); 
  19. item.setDescription( "Description item XYZ" ); 
  20. //Fill the response 
  21. itemActionResponse.setRetCode( "OK" ); 
  22. itemActionResponse.setItem(item); 
  23. Marshaller marshaller = jaxbContext.createMarshaller(); 
  24. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, 
  25. Boolean.TRUE); 
  26. //The following line is not required, it was inserted
  27. //just to see the content of the generated XML message 
  28. marshaller.marshal(itemActionResponse, System.out); 
  29. //Send the XML message to the client 
  30. marshaller.marshal( itemActionResponse, 
  31. response.getOutputStream()); 
  32. catch  (JAXBException e){ 
  33. throw   new  ServletException(e); 
  34. }
  35. }

到现在为止,我们完成了一个基本服务的实现,其工作流程非常明了:

1.        XML 请求序列化

2.        进行处理操作

3.        准备和序列化应答 XML

请注意上面的服务可供任何语言和技术调用,只要客户端程序能够对 XML 进行序列化和反序列化及对信息交换的协议有所了解即可。

  程序清单5 —itemCrudService 服务的客户端实现

 

  1. //Prepare the request
  2. ItemAction itemAction =  new  ItemAction();
  3. Item item =  new  Item();
  4. item.setId(26);
  5. itemAction.setMethod( "findById" );
  6. itemAction.setItem(item);
  7. //Prepare and establish the connection with the service
  8. URL url =  new  URL( "http://localhost/SoaBookPoxHttp/itemCrudService" );
  9. HttpURLConnection con = (HttpURLConnection) url.openConnection();
  10. con.setDoOutput( true );
  11. //Set the HTTP request method
  12. con.setRequestMethod( "POST" );
  13. con.connect();
  14. JAXBContext jaxbContext = JAXBContext.newInstance (ItemAction. class , ItemActionResponse. class );
  15. Marshaller marshaller = jaxbContext.createMarshaller();
  16. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  17. //The following line is not required, it was inserted 
  18. //just to see the content of the generated XML message
  19. marshaller.marshal(itemAction, System. out );
  20. //Send the XML request to the service
  21. marshaller.marshal(itemAction, con.getOutputStream()); 
  22. //Get the XML response from the service and deserialize it
  23. Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
  24. ItemActionResponse itemActionResponse = (ItemActionResponse) 
  25. unmarshaller.unmarshal(con.getInputStream());
  26. //Show the response content
  27. System. out .println( "retCode=" +itemActionResponse.getRetCode()+  "\r"  + 
  28. "id=" +itemActionResponse.getItem().getId()+  "\r"  + 
  29. "code=" +itemActionResponse.getItem().getCode()+  "\r" + "description=" +itemActionResponse.getItem() .getDescription());

通过以上代码您会看到,所有参与消息交换的类

( 包括 Item, ItemAction ItemActionResponse) 对客户端必须是可见的。在本例中,客户端和服务器端都使用 Java ,因此我们只需要简单地这些类从服务器端的项目中拷贝到客户端的项目中即可。但一般说来,这并不是必需的 ( 请思考一下如果客户端和服务器端使用不同语言的情况 ) 。服务实现过程中唯一的要求就是,您要传输的对象必须满足序列化和反序列化的要求。我们可以使用同样的方法来实现 findAllItems 服务,为此,我们新建一个 Servlet ,这个 Servlet 不需要任何输入,并返回所有的商品列表。该服务的实现代码如下:

 

程序清单 6—findAllItems 服务的服务器端实现

 

  1. protected   void  doPost(HttpServletRequest request, 
  2. HttpServletResponse response) 
  3. throws  ServletException, IOException
  4. try  { 
  5. JAXBContext jaxbContext = JAXBContext.newInstance (ItemList. class , Item. class ); 
  6. ItemList itemList =  new  ItemList(); 
  7. itemList.setList( new  ArrayList()); 
  8. Item i1 =  new  Item(); 
  9. i1.set ... ; 
  10. itemList.getList().add(i1); 
  11. ... populate itemList ... 
  12. Marshaller marshaller = jaxbContext.createMarshaller(); 
  13. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, 
  14. Boolean.TRUE); 
  15. //Just to see the content of the generated XML message 
  16. marshaller.marshal(itemList, System.out);
  17. //Send the XML message to the client 
  18. marshaller.marshal(itemList, response.getOutputStream()); 
  19. catch  (JAXBException e) { 
  20. throw   new  ServletException(e); 
  21. }
  22. }

请注意,这里我们还需要定义一个

ItemList 类,其代码如下:

 

程序清单 7—ItemList 类的源代码

 

  1. import  java.util.List;
  2. import  javax.xml.bind.annotation.XmlRootElement;
  3. @XmlRootElement (name= "ItemList"
  4. public   class  ItemList { 
  5. private  List list; 
  6. ...

相应的客户端测试代码应如下所示:

 

  程序清单 8— findAllItems 服务的客户端端测试代码

 

  1. URL url =  new  URL( "http://localhost/SoaBookPoxHttp/findAllItems" );
  2. HttpURLConnection con = (HttpURLConnection) url.openConnection();
  3. con.setRequestMethod( "POST" );
  4. con.connect();
  5. //Void Request
  6. //Get Response
  7. JAXBContext jaxbContext = JAXBContext.newInstance (ItemList. class , Item. class );
  8. Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
  9. ItemList itemList = (ItemList) 
  10. unmarshaller.unmarshal(con.getInputStream());
  11. for  (Iterator iterator = itemList.getList().iterator(); 
  12. iterator.hasNext();)
  13. Item item = (Item) iterator.next(); 
  14. System.out.println( item.getId()+ " - " + item.getCode()+ " - "
  15. item.getDescription());
  16. }

译者注:如果您想增加对

SOA 服务的理解,并有兴趣动手一试,请不妨补齐上面的源代码,看看能否在 Tomcat 中运行并通过测试。译者补齐了源代码并在 Tomcat6.0+Java6 的环境下测试通过,源代码的链接如下,供您参考。

http://carllgc.blog.ccidnet.com/job-htm-action-download-itemid-777373-aid-86781.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
XML安全教程 XML是确保Web服务安全的一个重要因素。XML是因特网以及近来Web服务持续增长和开发的主要支持者。但是,在实现XML语言的全部能力之前,还有许多与安全性相关的工作要做。目前,加密整个XML文档、测试其完整性和确认其发送方的可靠性是一个简单的过程。但是,越来越有必要对文档的某些部分也使用这些功能,以便以任意顺序加密和认证以及涉及不同用户或发起方。目前,在与XML相关的安全性领域方面开发规范的最重要部分是XML加密、XML签名、XKMS(W3C)和XACML、SAML(OASIS)。本专题汇集了与这些规范相关的文章,供大家参考。 XML安全术语 2003年初,OASIS小组批准了安全性断言标记语言(Security Assertion Markup Language,SAML)规范。由于来自25家公司的55名专家参与了该规范的制定,因此人们会认为SAML能做任何事情,并且能被很好地理解。但事实并非如此,软件开发社区存在着很多对SAML的误解。当SAML 2.0开始向Web 2.0接近如何让SAML适应你的SOA安全方案安全声明标记语言(SAML)的应用 XML安全介绍 XML加密为需要结构化数据安全交换的应用程序提供了一种端到端安全性。XML本身是对数据进行结构化最流行的技术,因此基于XML的加密成为处理数据互换应用程序中安全性的复杂需求的方法。XML资源Concordia和XML安全协同工作能力即将到来的入侵:XML会占据企业网络吗? XML安全应用 作为一种Internet上的信息交换格式,XML的普及性仍然在增长——而与信息交换有关的一个重要问题是安全。没有保证信息的安全性和可靠性的机制,任何信息交换格式都是不完整的。如何保证SOA安全用XML解决SOA的数据治理关注Web 服务安全 警惕黑客攻击
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值