SOA服务的基本实现方法—使用HTTP协议传输XML请求(POX-over-HTTP)

翻译自"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类来实现客户和服务器端的交互,这些类包括ItemItemActionItemActionResponse。它们都是带有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; ...

下面的代码是服务实现的主要部分,所有实现代码都包含在ServletdoPost()方法中,该ServletWeb.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, ItemActionItemActionResponse)对客户端必须是可见的。在本例中,客户端和服务器端都使用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
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值