顯示具有 SOA 標籤的文章。 顯示所有文章
顯示具有 SOA 標籤的文章。 顯示所有文章

2011年4月23日 星期六

[Web Service]-利用Eclipse及CXF2開發Web Service

準備工具
Eclipse IDE for Java EE Developer-目前版本Helios SR2
Tomcat-目前版本-7.0.12
Apache CXF2-目前版本2.4.0

開發環境配置
1.將下載的Eclipse、Tomcat及CXF2 zip檔解壓縮
2.執行Eclipse,點選Windows->Preferences->Web Services->CXF  2.x Preferences
   將CXF Runtime指向解壓縮後的CXFf2目錄,另外在Java2WS及WSDL2Java的tag中,
   也有一些屬性值可以加以設定,比如在Java2WS中我們就能設定是否要自動產生WSDL檔
3.在下方工作區點選Server tag,並在空白處按右鍵New->Server
    在這裡我們選擇Server類型-Apache Tomcat 7.0
按下一步,將目錄位置指向剛剛所解壓縮的Tomcat 7.0.12
按下確認後即可在Server區看到剛剛所新增的Tomcat Server


開發Web Service-Server端
1.新增一個Dynamic Web Project
2.新增一個EchoTest類別,裡面實作一個echo operation
3.在EchoTest類別上按右鍵選擇Web Service->Create Web Service
   在跳出的視窗中,Web Service Type這裡要選擇Button up,
   (Top down是指由WSDL來建立Web Service)
   另外Web service runtime這裡要選擇Apache CXF 2.x
按下確認後就會利用CXF套件幫我們產出Web Service的相關檔案了,連WSDL檔也一併產生
另外若當初有佈署Runtime Server的話,產生Web Service的同時也會將此Web Service佈署在此server中,如下圖在Tomcat Server下可以看到我們剛剛所產生的Web Service,按下server執行鈕即可運行該服務

開發Web Service-Client端
1.新增一個Java Project
2.在src按右鍵New->Other,在Web Services類別選擇Web Service Client
在跳出的視窗中點選Brower,在跳出視窗的上方URI欄位中輸入
http://localhost:8080/WSService/services/EchoTestPort?wsdl
這裡的WSDL位址在剛剛建立Web Service自動產出的WSDL文件中可以找到
按下確認後套件會自動找尋這份WSDL文件(注意:這裡Web Service所在的server必須啟動)
按下確認後回到前一個視窗,這裡的Web server runtime採用預設的Apache Axis就好
按下確認,套件會為我們自動產生Web Service Client的相關類別
3.我們新增一個EchoDemo類別來實際使用套件為我們產生的Web Service Client相關類別
4.直接執行,在client端就會利用套件為我們產生的Web Service Client相關類別來連接我們佈署在Tomcat Server上的Web Service,並帶回執行結果

2010年5月30日 星期日

[Web Service]-NetBeans開發Web Service

Getting Started with JAX-WS Web Services
                                                          from NetBeans Web Service tutorials
Java API for XML Web Services (JAX-WS) 2.0/2.1, JSR 224, is an important part of the Java EE 5 platform. A follow-up to the release of Java API for XML-based RPC 1.1(JAX-RPC), JAX-WS simplifies the task of developing web services using Java technology. It addresses some of the issues in JAX-RPC 1.1 by providing support for multiple protocols such as SOAP 1.1, SOAP 1.2, XML, and by providing a facility for supporting additional protocols along with HTTP. JAX-WS uses JAXB 2.0 for data binding and supports customizations to control generated service endpoint interfaces. With its support for annotations, JAX-WS simplifies web service development and reduces the size of runtime JAR files.

Creating a Web Service
Choosing a Container
You can either deploy your web service in a web container or in an EJB container. This depends on your choice of implementation. If you are creating a Java EE 6 application, use a web container in any case, because you can put EJBs directly in a web application. For example, if you plan to deploy to the Tomcat Web Server, which only has a web container, create a web application, not an EJB module.
  1. Choose File > New Project (Ctrl-Shift-N). Select Web Application from the Java Web category or EJB Module from the Java EE category.
  2. Name the project CalculatorWSApplication.
  3. Click through the remaining pages and click Finish.
Creating a Web Service from a Java Class
  1. Right-click the CalculatorWSApplication node and choose New > Web Service.
  2. Name the web service CalculatorWS and type org.me.calculator in Package.
  3. If you are creating a Java EE 6 project, leave Create Web Service from Scratch selected, and select Implement Web Service as a Stateless Session Bean.
  4. Click Finish. The Projects window displays the structure of the new web service and the source code is shown in the editor area.
Designing the Web Service
Adding an Operation to the Web Service
  1. Change to the Design view. (Note: The Preview view in NetBeans 6.5.1 is disabled.)
  2. Click Add Operation in the visual designer. A dialog box appears where you can define the new operation.
  3. In the upper part of the Add Operation dialog box, type add in Name and type int in the Return Type drop-down list. In the lower part of the Add Operation dialog box, click Add and create a parameter of type int named i. Then click Add again and create a parameter of type int called j. You now see the following:
  4. Click OK at the bottom of the Add Operation dialog box. The visual designer now displays the following:
  5. Click Source and view the code that you generated in the previous steps. It differs whether you created the service as an EE6 stateless bean or not. Can you see the difference in the screenshots below?
  6. In the editor, extend the skeleton add operation to the following (changes are in bold):
@WebMethod
public int add(@WebParam(name = "i") int i, 
@WebParam(name = "j") int j) {
int k = i + j;
return k;
} 
Deploying and Testing the Web Service
To test successful deployment to a web container:
  1. Right-click the project and choose Deploy. The IDE starts the application server, builds the application, and deploys the application to the server. You can follow the progress of these operations in the CalculatorWSApplication (run-deploy) and GlassFish or Tomcat tabs in the Output view.
  2. If you deployed to the Tomcat Web Server, you will see the following instead, which indicates that you have successfully deployed your web service:

Consuming the Web Service
Client : Java Class in Java SE Application
  1. Choose File > New Project (Ctrl-Shift-N). Select Java Application from the Java category. Name the project CalculatorWS_Client_Application. Leave Create Main Class selected and accept all other default settings. Click Finish.
  2. Right-click the CalculatorWS_Client_Application node and choose New > Web Service Client.
  3. In Project, click Browse. Browse to the web service that you want to consume. When you have selected the web service, click OK.
  4. Leave the other settings at default and click Finish. The Projects window displays the new web service client, with a node for the add method that you created:
  5. Double-click Main.java so that it opens in the Source Editor. Delete the TODO comment and then drag the add node above into the empty line. You now see the following:
public static void main(String[] args) {
try {
// Call Web Service Operation
org.me.calculator.CalculatorWSService service = 
new org.me.calculator.CalculatorWSService();
org.me.calculator.CalculatorWS port = 
service.getCalculatorWSPort();
// TODO initialize WS operation arguments here
int i = 0;
int j = 0;
// TODO process result here
int result = port.add(i, j);
System.out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here
}
}

2010年5月29日 星期六

[Web Service]-淺談Web Service

Introduction to Web Services
                                                                      from NetBeans Web Service tutorials
Web services are distributed application components that are externally available. You can use them to integrate computer applications that are written in different languages and run on different platforms. Web services are language and platform independent because vendors have agreed on common web service standards.
Several programing models are available to web service developers. These models fall into two categories, both supported by the IDE:
  • REST-based. REpresentational State Transfer is a new way to create and communicate with web services. In REST, resources have URIs and are manipulated through HTTP header operations.
  • SOAP/WSDL-based. In traditional web service models, web service interfaces are exposed through WSDL documents (a type of XML), which have URLs. Subsequent message exchange is in SOAP, another type of XML document.
RESTful Web Services
REST-based ("RESTful") web services are collections of web resources identified by URIs. Every document and every process is modeled as a web resource with a unique URI. These web resources are manipulated by the actions that can be specified in an HTTP header. Neither SOAP, nor WSDL, nor WS-* standards are used. Instead, message exchange can be conducted in any format—XML, JSON, HTML, etc. In many cases a web browser can serve as the client.

HTTP is the protocol in REST. Only four methods are available: GET, PUT, POST, and DELETE. Requests can be bookmarked and responses can be cached. A network administrator can easily follow what is going on with a RESTful service just by looking at the HTTP headers.

REST is a suitable technology for applications that do not require security beyond what is available in the HTTP infrastructure and where HTTP is the appropriate protocol. REST services can still deliver sophisticated functionality. Flickr, Google Maps and Amazon all provide RESTful web services. NetBeans IDE Software as a Service (SaaS) functionality lets you use Facebook, Zillow, and other third-party-provided services in your own applications.

SOAP-based Web Services
In SOAP-based web services, Java utilites create a WSDL file based on the Java code in the web service. The WSDL is exposed on the net. Parties interested in using the web service create a Java client based on the WSDL. Messages are exchanged in SOAP format. The range of operations that can be passed in SOAP is much broader than what is available in REST, especially in security.

SOAP-based web services are suitable for heavyweight applications using complicated operations and for applications requiring sophisticated security, reliability or other WS-* standards-supported features. They are also suitable when a transport protocol other than HTTP has to be used. Many of Amazon's web services, particularly those involving commercial transactions, and the web services used by banks and government agencies are SOAP-based.

The Java API for XML Web Services (JAX-WS) is the current model for SOAP-based web services in Metro. JAX-WS is built on the earlier JAX-RPC model but uses specific Java EE 5 features, such as annotations, to simplify the task of developing web services. Because it uses SOAP for messaging, JAX-WS is transport neutral. It also supports a wide range of modular WS-* specifications, such as WS-Security and WS-ReliableMessaging.