EJB Web Services


EJB3.0暴露對談EJB作為Web服務提供選項。@WebService注釋是用來標記一類作為一個Web伺服器端點,並使用@WebMethod是用來作為用戶端的Web方法的公開方法。

@Stateless
@WebService(serviceName="LibraryService")
public class LibraryPersistentBean implements LibraryPersistentBeanRemote {
	
   ...
   @WebMethod(operationName="getBooks")
   public List<Book> getBooks()  {    
      return entityManager.createQuery("From Books").getResultList();
   }
   ...
}

範例應用程式

讓我們建立一個測試EJB在EJB3.0應用程式來測試BLOB/CLOB支援。

Step Description
1 Create a project with a name EjbComponent under a package com.tutorialspoint.entity as explained in the EJB - Create Application chapter. Please use the project created in EJB - Persistence chapter as such for this chapter to understand clob/blob objects in ejb concepts.
2 Create LibraryPersistentBean.java under package com.tutorialspoint.stateless. Use EJB - Persistence chapter as reference. Keep rest of the files unchanged.
3 Clean and Build the application to make sure business logic is working as per the requirements.
4 Finally, deploy the application in the form of jar file on JBoss Application Server. JBoss Application server will get started automatically if it is not started yet.

LibraryPersistentBean.java

package com.tutorialspoint.stateless;

import com.tutorialspoint.entity.Book;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
@WebService(serviceName="LibraryService")
public class LibraryPersistentBean implements LibraryPersistentBeanRemote {
    
   public LibraryPersistentBean(){
   }

   @PersistenceContext(unitName="EjbComponentPU")
   private EntityManager entityManager;         

   public void addBook(Book book) {
      entityManager.persist(book);
   }    
   
   @WebMethod(operationName="getBooks")
   public List<Book> getBooks() {
      return entityManager.createQuery("From Book").getResultList();
   }
}

JBoss應用伺服器的紀錄檔輸出

10:51:37,271 INFO  [EJBContainer] STARTED EJB: com.tutorialspoint.stateless.LibraryPersistentBean ejbName: LibraryPersistentBean
10:51:37,287 INFO  [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:

	LibraryPersistentBean/remote - EJB3.x Default Remote Business Interface
	LibraryPersistentBean/remote-com.tutorialspoint.stateless.LibraryPersistentBeanRemote - EJB3.x Remote Business Interface

10:51:37,349 INFO  [EJBContainer] STARTED EJB: com.tuturialspoint.messagebean.LibraryMessageBean ejbName: BookMessageHandler
10:51:37,443 INFO  [DefaultEndpointRegistry] register: jboss.ws:context=EjbComponent,endpoint=LibraryPersistentBean
10:51:38,191 INFO  [WSDLFilePublisher] WSDL published to: file:/D:/Jboss-5.0.1/server/default/data/wsdl/EjbComponent.jar/
LibraryService3853081455302946642.wsdl

建立用戶端存取EJB作為Web服務

在NetBeansIDE中,選擇File>NewProject>類別下選擇專案型別,Java專案型別為Java應用。點選Next>按鈕。輸入專案的名稱和位置。單擊“Next>“按鈕。我們選擇名為EJBWebServiceClient。

右鍵點選專案名稱在專案exporer視窗中。選擇 New > WebService Client .

WSDL Client

新增EJB元件專案的LibraryPersistentBean早下建立WSDL和用戶端位置使用在"編譯"索引標籤中新增專案“按鈕。

Web Service Bean

單擊“完成”按鈕。在專案資源管理器驗證以下結構。

Web Service Bean

Create EJBWebServiceClient.java

package ejbwebserviceclient;

public class EJBWebServiceClient {
   public static void main(String[] args) {   
   }
}

如下圖所示,選擇Web服務getBooks Web方法,將其拖動到程式碼視窗EJBWebServiceClient。

Web Service Method drag

會看到類似的輸出如下所示。

Web Service Method dragged

更新EJBWebServiceClient的程式碼使用此方法。

package ejbwebserviceclient;

public class EJBWebServiceClient {

   public static void main(String[] args) {
      for(com.tutorialspoint.stateless.Book book:getBooks()){
         System.out.println(book.getName());
      }       
   }

   private static java.util.List
   <com.tutorialspoint.stateless.Book> getBooks() {
      com.tutorialspoint.stateless.LibraryService service = 
         new com.tutorialspoint.stateless.LibraryService();
      com.tutorialspoint.stateless.LibraryPersistentBean port = 
         service.getLibraryPersistentBeanPort();
      return port.getBooks();
   }      
}

執行用戶端

右鍵點選專案名稱,專案資源管理器視窗。選擇“Run”。 NetBeans將生成用戶端並執行它。驗證下面的輸出。 

ant -f D:\SVN\EJBWebServiceClient run
init:
Deleting: D:SVNEJBWebServiceClientuilduilt-jar.properties
deps-jar:
Updating property file: D:SVNEJBWebServiceClientuilduilt-jar.properties
wsimport-init:
wsimport-client-LibraryPersistentBean:
files are up to date
classLoader = java.net.URLClassLoader@4ce46c
SharedSecrets.getJavaNetAccess()=java.net.URLClassLoader$7@182cdac
wsimport-client-generate:
Compiling 1 source file to D:SVNEJBWebServiceClientuildclasses
compile:
run:
learn java
Learn Spring
learn JSF
Learn HTML
Learn JBoss
Learn EJB
Learn Hibernate
Learn IBatis
Times Now
learn html5
Learn images
Learn Testing
Forbes
test1
BUILD SUCCESSFUL (total time: 1 second)