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來實現這個服務。<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

我們就動手寫服務器和客戶端的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

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章