SCN Wiki upgrade to version 6.13 on Tuesday 19.11.2019 - beginning 07:30 am CET

During upgrade process new content will not be transported to new Wiki version

Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Goal:

To consume SOAP web service in java. We had a use case to consume SOAP services developed in ABAP in Java for our UI applications. To generate Java clients from WSDL, Apache CXF is used.

Prerequisite:

  1. Download Eclipse IDE for JEE developers.( http://www.eclipse.org/downloads/ )
  2. Download Apache CXF.( http://cxf.apache.org/download.html (Binary distribution .ZIP file))
  3.  Download Apache Tomcat. (http://tomcat.apache.org/ )
  4.  Download and install JDK.( http://www.oracle.com/technetwork/java/javase/downloads/index.html)
  5. Configure Installed JDK in Eclipse.
    To configure installed JDK in eclipse, go to Windows->Preferences->Java->Installed JRE, Click  'Add' button, choose 'Standard VM' in Add JRE window and click next. In the next screen, select the path where JDK was installed using 'Directory' button
     

  6. The Apache Tomcat server, which will host the web service, needs to be configured in the Eclipse IDE.
    To configure tomcat in eclipse, select Windows->Preferences->Server->Runtime Environments. Click 'Add' button, choose relevant Apache Tomcat server and select the Tomcat installation directory and select finish.


  7. The Eclipse IDE needs to be configured to use the Apache CXF web service framework for creating and communicating with web services.

     To configure Apache CXF,Select  Window –>Preferences –>Web Services –>CXF 2.x Preferences. Under “CXF Runtime” select “Add” and select the CXF installation directory and click finish.

  8. Finally, select “Server and Runtime” under “Web Services” and set the Server Runtime to Tomcat 7.0 and the Web Service Runtime as Apache CXF 2.x.

Steps to create Java client from WSDL

  1. Download the WSDL file from service provider.
  2. Create dynamic web project in eclipse by selecting File->New->Dynamic Web project.

    In the wizard, select Target runtime as 'Apache Tomcat 7.0', Dynamic web module version as '3.0' and Configuration as 'Default Configuration for Apache Tomcat v7.0'. Click next button, again click next and in web module window check option 'Generate web.xml deployment descriptor' and click finish.

  3. Copy downloaded WSDL file to web-inf  folder under Web Content folder. Right click on wsdl file, choose web service-> Generate Client option

  4. In the Web service client wizard select client type as 'Java Proxy' and make sure under configurations for Server runtime  'Tomcat v7.0 server' is selected and for Web service runtime 'Apache CXF 2.X' is selected. Move the slider bar to 'Develop client'. Click next button and in next window, make sure there is no '-' (hypen in the package name (as '-' is not supported in java namespace) and click finish.

  5. Check java classes got generated in Java Resources->src->(package_name) under the dynamic web project.

    If you are using JDK1.8.0 and while generating java client if you face the following exception, "org.xml.sax.SAXParseException; schema_reference: Failed to read schema document 'xml.xsd', because 'file' access is not allowed due to restriction set by the accessExternalSchema property".

    Create a file named jaxp.properties (if it doesn't exist) under /path/to/jdk1.8.0/jre/lib and then write this line in it:

    javax.xml.accessExternalSchema = all 

  6. In the generated client java class, add the following code in main() method at the beginning.

    org.apache.cxf.jaxws.JaxWsProxyFactoryBean factory = new org.apache.cxf.jaxws.JaxWsProxyFactoryBean();

    factory.getInInterceptors().add(new LoggingInInterceptor());

    factory.getOutInterceptors().add(new LoggingOutInterceptor());

     // Utilize the class which was auto-generated by Apache CXF wsdl2java for service interface

    factory.setServiceClass("SERVICEINTERFACECLASSNAME".class);

    // Use the endpoint URL for your web service

    factory.setAddress("ENDPOINT_URL");

     

    SERVICEINTERFACECLASSNAME port = (SERVICEINTERFACECLASSNAME) factory.create();

     

    org.apache.cxf.endpoint.Client client = org.apache.cxf.frontend.ClientProxy.getClient(obj);

    org.apache.cxf.transport.http.HTTPConduit httpConduit = (org.apache.cxf.transport.http.HTTPConduit) client.getConduit();

    org.apache.cxf.configuration.security.AuthorizationPolicy authorization = httpConduit.getAuthorization();

    authorization.setUserName(USER_NAME);

    authorization.setPassword(PASS_WORD);

    long timeout = 10000L;

    org.apache.cxf.transports.http.configuration.HTTPClientPolicy policy = new org.apache.cxf.transports.http.configuration.HTTPClientPolicy();

    policy.setConnectionTimeout(timeout);

    policy.setReceiveTimeout(timeout);

    httpConduit.setClient(policy);

     

  7. Do the mapping to send the request to web service and call the method by

    port.methodname(request);

     

  8. Run the client class as java application.

    If the server certificate is not installed in your system, you would get following exception

    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.
    provider
    .certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

     In that case do the following

    Get a InstallCert.java file from 

     http://code.google.com/p/java-use-examples/source/browse/trunk/src/com/aw/ad/util/InstallCert.java

    Compile InstallCert.java.

     Run InstallCert.java, with your hostname and https port, and press “1” when ask for input in command prompt. It will add your hostname as a trusted keystore, and generate a file named “jssecacerts“.

     This application InstallCert.java was originally written for Java 5 / Java 6. While this solution still works, when you try to run this application through Java 7 or above, you will get an additional error message like this:

    javax.net.ssl.SSLException: java.lang.UnsupportedOperationException.

    To avoid this error, you will need to change how accepted issuers are returned in your java class "InstallCert.java". The original code threw an exception for this method. The Java 7 implementation calls this method, which was not the case with earlier versions of Java. The solution to this problem is to simply return an empty array like this:

    @Override

    public X509Certificate[] getAcceptedIssuers() {

        return new X509Certificate[0];

        // throw new UnsupportedOperationException();

    }

     Copy the generated “jssecacerts” file to your “$JAVA_HOME\jre\lib\security” folder.

    Done

    Run your web service client again, it should be working now.





  • No labels

2 Comments

  1. Very helpful article Ashok!

  2. Great article! Simple to understand and do hands on!