Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
psnn24
Explorer
0 Kudos

1. Overview

The Objective of this tutorial is to give you a walkthrough on how to create a web service for generating an Xml query, posting it to a server and getting the response. The created web service will be provided with a WSDL URL, which can be used in any application for consuming the web service.

The case here considered for creating the web service is tracking an item based on its GTIN and Serial Number or Barcode. Based on the GTIN and Serial Number a URN is formed and is given as input along with Query Type and Action Type to the web service, which returns the XML form of the query.

Let us consider ‘SimpleEventQuery’ as the Query Type and ‘MATCH_epc’ as the Action Type with the product having GTIN 10066800543215 and Serial Number 1308. The URN formed for the action ‘MATCH_epc’ will look like [urn:epc:id:sgtin:0066800.054321.1308]. And the XML form of query will be,

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:urn="urn:epcglobal:epcis-query:xsd:1">

<soapenv:Header/>

<soapenv:Body>

<urn: Poll>

<queryName>SimpleEventQuery</queryName>

<params>

<param>

<name>MATCH_epc</name>

<value>

<string>urn:epc:id:sgtin:0066800.054321.1308</string>

</value>

</param>

</params>

</urn: Poll>

</soapenv:Body>

</soapenv:Envelope>

Now, the generated XML query will be posted to server. To do this Server URL, UserName, Password along with the XML query are passed to web service as inputs and web service returns a string containing Response Number, Response and a Message separated by a delimiter “@@@”.




2. Prerequisites

The libraries are required for using HTTP POST method.

  • Should have created an Enterprise Application (EAR) Project and Added an External Library DC, containing the downloaded library (.jar) files as used DC.

Click here to know how to create EAR with External Library DC added to it.

The EAR application httpLibEAR with public part httplib is considered here for explanation purpose.

  • Should have access to the Application Server and configured the same in NWDS.
  • Should have a message server(Object Event Repository) details and credentials for the same. And Application Server should be interfaced to message server.

 

3. Procedure for Web Service Creation

3.1. Creating an Enterprise Java Bean project For the Web Service

  • In the NWDS, select FileNewOther

  

  • Select Development ComponentNext

 

  • Select Java EE EJB Module Next

 

  • Give the name as querypostejb and click Finish. And confirm that you wish to switch perspectives if it asks.
  • In the Project Explorer, copy the downloaded library (.jar) files under WEB-INF/lib

 

3.2. Creating an Enterprise Application project For the Web Service

  • In the NWDS, select FileNewOther

 

  • Select Development ComponentNext

 

  • Select Java EE Enterprise Application Next

 

  • Give the name as querypostear and click Next Next.

 

  • Select querypostejb project as the Referenced Project and click Finish.

  • In the Project Explorer, copy the downloaded library (.jar) files under lib folder

 

  • Select the project querypostear Component Properties Dependencies Add

Select httpLibEAR under My Components and select Finish.

Repeat the same steps for querypostejb and add httpLibEAR to Dependencies.

If you are not able to see Component Properties in NWDS window,

Go to Windows Show View Other → select Component Properties and click OK




3.3. Adding Libraries Required to Enterprise Java Bean Project

  • Right Click on the project querypostejb Properties Java Build Path Libraries
    • Add JARs → Add the jar files copied under WEB-INF/lib and click OK.
    • Add Library → EAR Libraries → Next → Finish

Then click OK.

This is the important step. If you miss this step you will get error during either build time or while deploying.

 

3.4. Creating the Enterprise Java Bean

  • In the Project Explorer, Right Click on the querypostejb project, and then choose New.

  • In the next wizard, give Java Package as com.sap.tutorial.querypostejb and class name as QueryPost
  • In the Create Business Interface area, select Remote and Local, and then choose Finish.
  • Update the source code as shown in the sample below.

 

@Stateless(name = "QueryPost")

public class QueryPost implements QueryPostRemote, QueryPostLocal {

//@@begin javadoc:createXML()

  /**

   * Take the inputs and create the xml query

   * @Param String query, String action, String urn

   * @return String xml

   */

  //@@end

  public String createXML(String query, String action, String urn )  {

    //@@begin createXML()

      StringBuffer xml = new StringBuffer(); //to create the xml

                                                                  /*=====generate XML=======*/

      xml.append("<?xml version='1.0' encoding='UTF-8'?>\n")

      .append("<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'\n")

      .append("xmlns:urn='urn:epcglobal:epcis-query:xsd:1'>\n")

      .append("<soapenv:Header/>\n")

      .append("<soapenv:Body>\n")

      .append("<urn:Poll>\n")

      .append("<queryName>")

      .append(query)

      .append("</queryName>\n")

      .append("<params>\n")

      .append("<param>\n")

      .append("<name>")

      .append(action)

      .append("</name>\n")

      .append("<value>\n")

      .append("<string>")

      .append(urn)

      .append("</string>\n")                  

      .append("</value>\n")

      .append("</param>\n")

      .append("</params>\n")

      .append("</urn:Poll>\n")

      .append("</soapenv:Body>\n")

      .append("</soapenv:Envelope>");      

      return xml.toString();  //return generated xml in string format

      //@@end

      }

//@@begin javadoc:postXML()

/**

   * Take the XML query, Post it to the server and get response

   * @Param String xml_query, String url, String userName, String password

   * @return reply

   */

  //@@end

     

 

  @SuppressWarnings({ "finally", "deprecation" })

public String postXML( String xml_query, String url, String userName, String password )  {

  //@@begin postXML()

  String response = null; //to store the response from server

  int result = 0;

  String message = null;

  String reply = null;

       

  PostMethod post = new PostMethod(url); //initialize post method

  post.setRequestHeader("Content-type", "text/xml; charset=UTF-8"); //set content type i.e. XML

  post.setRequestBody(xml_query); //set query string to be posted

                   

// Get HTTP client

  HttpClient httpclient = new HttpClient();

httpclient.getParams().setAuthenticationPreemptive(true);

      /*==authentication==*/

Credentials defaultcreds = new UsernamePasswordCredentials(userName, password);

  1. httpclient.getState().setCredentials(AuthScope.ANY, defaultcreds);

      

   try {

      //post the XML execute and get response number

      result = httpclient.executeMethod(post);                   

      response = post.getResponseBodyAsString(); //get the response text

      if(result==200){

            message = "Success";

      }else{

            message = "Error: "+Integer.toString(result);

      }

} catch (HttpException e) {

      response = "null";

      message = "Could not establish connection!";

                   

} catch (IOException e) {

      response = "null";

      message = "Could not post your query, please try after sometime";

                   

} finally {

// Release current connection to the connection pool once you are done

                post.releaseConnection();

           

//Append result, response and message to the return string with delimiter "@@@".

                reply = result+"@@@"+response+"@@@"+message;

                return reply;

            }

                   

          //@@end

        }

}

There are two methods. CreateXML() and PostXML().

     The XML Query is generated in the method CreateXML() & PostXML() method is used to post the generated XML Query to message server and get the response.

  • In the Project Explorer, expand the querypostejb project, and then choose ejbModulecom.sap.tutorial.querypostejbQueryPost.javaQueryPost.
  • Select createXML( ) and postXML() and then Right Click →→

 

 

3.5. Exposing the Enterprise Java Bean as Web Service

  • In the Project Explorer, expand the querypostejb project, and then choose ejbModulecom.sap.tutorial.querypostejbQueryPost.java.
  • Right-click QueryPost.java, and then choose Web ServicesCreate Web Service.

The Web Services wizard appears.

  • Move the slider to Develop service position, as shown in the figure below.

   Choose Next.

The Service Endpoint Interface screen opens.

  • Choose Specify existing interface option, as shown in the figure below.
  • Choose Browse and then in the Select Service endpoint interface, enter com.sap.tutorial.queypostejb.QueryPostRemote.
  • From the Matching items area, choose the QueryPostRemote interface. When the QueryPostRemote interface is selected, the Web service annotations are generated in the remote Java interface.

     Choose Next.

  • In the Web Service customizations step, copy the WSDL URL Preview choose Finish.

3.6. Deploying the Web Service

Building and Deploying

  • In the Project Explorer, Right Click on querypostear


Testing the Web Service

  • Test your webservice from Net Weaver Web Services Navigator http:// <host>:<port/wsnavigator

1. References

  • How to Reference External JAR Files in Web Dynpro DC in SAP NW Portal 7.3.

http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/006a6229-b1ed-2e10-0c8c-cc5673cf2...

  • Java.lang.NoClassDefFoundError – Not Again?

http://scn.sap.com/community/web-dynpro-java/blog/2013/09/02/javalangnoclassdeffounderror--not-again

  • Consuming EJB’s in Web Dynpro Java for CE7.1. 

  http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/b030e7fb-2662-2b10-0dab-c4aa52c35...


1. Abbreviations

DC:                 Development Component

EAR:              Enterprise ARchive

EJB:               Enterprise Java Bean
EPC:
             Electronic Product Code

GTIN:            Global Trade Identification Number

HTTP:           Hyper Text Transfer Protocol

HTTPS:        Hyper Text Transfer Protocol Secure

JAR:              Java ARchive

NWDS:         Net Weaver Development Studio

URL:             Uniform Resource Locator

URN:            Uniform Resource Name

WSDL:         Web Services Description Language

XML:             Extensible Markup Language

OER:          Object Event Repository

Labels in this area