WebServices in Enterprise Java Bean V3

Below example demostractes how to expose EJB methods as WebServices

Below steps are to create WebServices in EJB 3 :

  • Create EJB Project EJBWebServices.
  • Create Stateless Session Bean with Local interface
  • Edit Source Code
  • Verify the project directory structure
  • Deploy EJB project on server
  • Open WSDL in browser
  • Run Test application for WebServices on WebLogic Server

Step 1: Create EJB Project EJBWebServices
File -> New -> EJB Project -
Web Services in EJB
Specify the EJB project name and click Next.

You will get below screen-
Web Services in EJB
Keep all options default and click Next.

You will get below screen -
Web Services in EJB
Click Finish.

Step 2: Create Stateless Session Bean class WebServicesImpl1 and local interface WebServices1-
Web Services in EJB
This step will create two java files. One will be local interface WebServices1.java and other will be bean class WebServicesImpl1.java.

Step 3: Edit Sourcecode
WebServices1.java
package techmyguru.webservices;
import javax.ejb.Remote;
import javax.jws.WebService;
@Remote
@WebService(name = "WebService1", targetNamespace = 
"http://techmyguru.webservices")
public interface WebServices1 
{
  public String welcome(String msg);
  public String addition(String num1, String num2);
  public String multiplication(String num1, String num2);
}

WebServicesImpl1.java
package techmyguru.webservices;

// all import statements

@Stateless(mappedName="ws1")
@WebService(serviceName = "WebService1", portName = "WebService1Port"
      endpointInterface = "techmyguru.webservices.WebServices1"
      targetNamespace = "http://techmyguru.webservices")
public class WebServicesImpl1 implements WebServices1 
{
  @WebMethod(operationName="welcome")
  public String welcome(@WebParam(name = "msg"String msg
  {
  return "Welcome to" + msg;
  }

  @WebMethod(operationName="addition")
  public String addition(@WebParam(name = "num1"
  String num1, @WebParam(name = "num2"String num2
  {
  String status = "";
  try 
  {
  int sum = Integer.parseInt(num1+ Integer.parseInt(num2);
  status = "The sum is =" + sum;
  
  catch (NumberFormatException e) {
  status = "Not valid Number" + e.getClass();
  }
  return status;
  }

  @WebMethod(operationName="multiplication")
  public String multiplication(@WebParam(name = "num1"
  String num1, @WebParam(name = "num2"String num2) {
  String status = "";
  try 
  {
  int multi = Integer.parseInt(num1* Integer.parseInt(num2);
  status = "The multiplication is "+multi;
  
  catch (NumberFormatException e) {
  status = "Not valid Number" + e.getClass();
  }
  return status;
  }
}

Step 4: Verify the final project directory structure as below -
Web Services in EJB

Setp 5: Deploy project on server
Right lick on EJB Project -> Run As -> Run on Server.

Step 6: Open WSDL in browser
Open below URL in browser to see auto generated WSDL -
Web Services in EJB

Step 7: Run Test application for WebServices on WebLogic Server
Login in Admin Console - http://localhost:7001/console

Click Deployments under Domain Structure -> +EJBWebServices -> Click WebService1.
You will get below sreen
Web Services in EJB

Click +WebService1 -> click Test client.
You will below screen
Web Services in EJB

And test business logic of web services


Previous Download Source Code Next


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code