Show TOC

Using Enterprise JavaBeans as Web Service EndpointsLocate this document in the navigation structure

Context

Web Services

Web services are Web-based enterprise applications that run across heterogeneous networks and exchange data using HTTP protocols and XML data formats. You can implement a Web service client on one machine and then call a service hosted somewhere across the Internet.

You can use an EJB as a Web service endpoint to access a Java EE application. The Web service client invokes the business methods of a stateless session bean.

Use a stateless session bean as a Web service endpoint if:

  • The business logic of the service is within an EJB layer, because in this case the endpoint and the Web service's business implementation reside in the same layer.

  • You want to use transaction support and security in the Web service. This is provided by the EJB container.

  • You need to execute some business logic that resides on the EJB layer before invoking Web services.

  • You want the container to take care of synchronizing concurrent access to your service.

WSDL

Web Services Description Language (WSDL) is an XML vocabulary for describing a Web service interface. WSDL defines the service, the set of operations on the server, the format of client invocations.

Procedure

  1. To use a session bean as the endpoint of a Web service, we recommend you include a remote interface in the session bean. This remote interface is the service endpoint interface and specifies what methods the Web service publishes.
    @Stateless
    
    public class ConverterBean implements ConverterRemote {
    
                   
  2. Use @WebService to annotate the bean class and @WebMethod to annotate the methods of the bean class. The @WebService annotation defines the class as a Web service endpoint. A service endpoint interface is a Java interface that declares the methods that a client can invoke on the service.
    @WebService(serviceName = "ConverterBeanService", name = "ConverterBean", 
                    targetNamespace = "http://sap.com/tutorial/javaee/", portName = "ConverterBeanPort")
    
    @Stateless
    public class ConverterBean implements ConverterRemote {
    
    ...
    
            @WebMethod(exclude = false, operationName = "dollarToEuro")
            public BigDecimal dollarToEuro(@WebParam(name = "dollars") BigDecimal dollars) {
    
    ...
    
                   
  3. To explicitly specify the service endpoint interface, add the endpoint interface element to the @WebService annotation in the implementation class. You must then provide a service endpoint interface that defines the available public methods in the endpoint implementation class.

    If you do not specify the endpoint interface element in the @WebService annotation, you can use only the methods annotated with @WebMethod annotation.

Example

package com.sap.javaee.ws;
import java.math.BigDecimal;
import javax.ejb.Stateless;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

@WebService(serviceName = "ConverterBeanService", name = "ConverterBean", 
                targetNamespace = "http://sap.com/tutorial/javaee/", portName = "ConverterBeanPort")

@Stateless
public class ConverterBean implements ConverterRemote {

        private BigDecimal dollarRate = new BigDecimal("1.2");
        private BigDecimal euroRate = new BigDecimal("0.83");

        @WebMethod(exclude = false, operationName = "dollarToEuro")
        public BigDecimal dollarToEuro(@WebParam(name = "dollars") BigDecimal dollars) {

                BigDecimal result = dollars.multiply(euroRate);
                return result.setScale(2, BigDecimal.ROUND_UP);

        }

        @WebMethod(exclude = false, operationName = "euroToDollar")
        public BigDecimal euroToDollar(@WebParam(name = "euros") BigDecimal euros) {

                BigDecimal result = euros.multiply(dollarRate);
                return result.setScale(2, BigDecimal.ROUND_UP);

        }
}

         
Annotation Reference

Name

Use

Target

Annotation Attribute

@WebService

This annotation defines a Java class as a Web service or a Java interface as a Web service interface.

TYPE

name - this is the name of the Web service. The default value is "" .

targetNamespace - this is the XML namespace used for the WSDL and XML elements generated from this Web service. The default value is "" .

serviceName - this is the service name of the Web service. The default value is "" .

wsdlLocation - the location of a predefined WSDL describing the service. The default value is "" .

endpointInterface - the complete name of the service endpoint interface defining the service's abstract Web service contract. If you use this annotation, the service endpoint interface is used to determine the abstract WSDL contract. The default value is "" .

@WebMethod

Use this annotation to customize a method that is exposed as a Web service operation.

METHOD

operationName - this is the name of the WSDL operation matching this method. The default value is "" .

action - this is the action for this operation. The default value is "" .

@WebService

Provider

Use it to annotate a Provider implementation class.

TYPE

wsldLocation - this is the location of the WSDL description for the service. The default value is "" .

serviceName - this is the service name. The default value is "" .

targetNamespace - this is the target namespace for the service. The default value is "" .

portName - this is the port name. The default value is "" .

@WebParam

Use this annotation to customize the mapping of an individual parameter to a Web service message part and XML element.

PARAMETER

name - this is the name of the parameter as it appears in the WSDL. The default value is "" .

targetNamespace - this is the namespace for the parameter. The default value is "" .

mode - this is the direction in which the parameter is flowing. The default value is IN .

header - if this attribute is true, the parameter is pulled from a message header rather than the message body. The default value is false .