Show TOC Start of Content Area

Procedure documentation Creating Web Service Client Applications  Locate the document in its SAP Library structure

Use

Use this procedure to invoke a Web service proxy in your application.

There are a few differences when consuming Web service proxies from standalone Java applications and from Java EE applications. In the first case, the proxy exists in a non managed environment and the caller is responsible for manually instantiating and configuring the proxy. In the case of Java EE applications, the proxy is consumed from an EJB or Web module and is created and configured by the responsible managing container. Therefore, to allow the container to create and configure the proxy, a Java EE injection mechanism has to be used. This is achieved with the @WebServiceRef(name="MyName").The string "MyName" has to be unique for the Enterprise JavaBean that uses the client or for the whole Web module.

Note

If you want to consume a WS-RM-enabled Web service, or a Web service which uses TU&C/C, In the application-j2ee-engine.xml file of the EAR containing the Web service client, add a runtime reference to the WS-RM application on the application server as shown in the code sample below:

<?xml version="1.0" encoding="UTF-8"?>

<application-j2ee-engine xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="application-j2ee-engine.xsd">

  <reference reference-type="hard">

    <reference-target target-type="application" provider-name="sap.com">tc~esi~esp~wsrm~app</reference-target>

  </reference>

</application-j2ee-engine>

The Web service client and the Web service proxy must be in the same EAR and in the same module (EJB or Dynamic Web module).

Note

If you use development components (DC) to provide Web services, you have to add references to the respective public DC.

Prerequisites

You have created a Web service proxy. More information: Creating Web Service Proxies

Procedure

...

       1.      Depending on your project and scenario you want to fulfill, create a new Java class, EJB, or servlet.

       2.      Retrieve an instance of the Web service proxy.

                            a.      For standalone Java applications:

CurrencyExchangeService service = new CurrencyExchangeService();

CurrencyExchangePortType port = service.getCurrencyExchangePort();

                            b.      For Java EE modules (EJB, Web), add the @WebServiceRef  annotation to the member variable holding a reference to the proxy: @WebServiceRef (name="ExchangeService") CurrencyExchangeService service;

       3.      Retrieve an instance of the configuration port:

       To invoke a default logical port on the Web service proxy:

CurrencyExchangePortType port = service.getPort(<SEI_Class_Name>)

This method enables you to invoke the logical port that is marked as default in the configuration provided by SOA configuration for connectivity in SAP NetWeaver Administrator. We recommend that you use this approach.

       To invoke an explicitly configured logical port on the Web service proxy:

CurrencyExchangePortType port = service.getCurencyExchangePort()

This method enables you to invoke a service endpoint that is created manually in Single Service Administration in SAP NetWeaver Administrator.

More information: Configuring Individual Web Service Clients

       4.      Optionally, you can set the properties for endpoint URL, username, and password.

javax.xml.ws.BindingProvider bp = (javax.xml.ws.BindingProvider)port;

Map<String,Object> context = bp.getRequestContext();

context.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://new.endpoint.example");

context.put(BindingProvider.USERNAME_PROPERTY,"userName");

context.put(BindingProvider.PASSWORD_PROPERTY,"pass");

Note

If you want to set system HTTP proxy settings, you can do so from the SAP NetWeaver Administrator. Those settings apply for the whole application server. More information: Configuring Proxy Settings.

       5.      Invoke the business methods from a servlet.

Example

//retrieve the Service instance in Java EE applications case

@WebServiceRef (name="ExchangeService")

CurrencyExchangeService service;

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

      CurrencyExchangePortType port = service.getCurrencyExchangePort();

 

      HTTPControlInterface httpControl = HTTPControlFactory.getInterface(port);

      httpControl.setHTTPProxy("http.proxyPort",8080);

      response.getWriter().println("Exchange rate between Germany  and Russia: ");

      response.getWriter().println(port.getRate("Germany", "Russia"));

   }

       6.      Invoke the Web service client application.

      For Java EE modules:

                            a.      Choose Window Show View Servers.

                            b.      Select SAP Server node and from the context menu, choose Add and Remove Projects.

                            c.      From the dialog box that is displayed, select the EAR project with the Web service client.

                            d.      Choose Finish.

                            e.      Select SAP server and from the context menu, choose Publish.

To invoke the servlet, open a Web browser and enter the following data:

http://<server>:<http port>/<webproject name>/<url-pattern>

<url-pattern> is a customizable property of the servlet. You can check its value in the web.xml file of the Web module. The default value of <url-pattern> is the value of <servlet-name> in the web.xml file.

      For standalone Java applications:

Run the Java class and the main method will be invoked.

The result is displayed in the Console view. The Console view is accessible from Windows Show View Console.

 

End of Content Area