NCBI Bookshelf. A service of the National Library of Medicine, National Institutes of Health.

Entrez Programming Utilities Help [Internet]. Bethesda (MD): National Center for Biotechnology Information (US); 2010-.

Bookshelf ID: NBK55696

Using Entrez Utilities Web Service with Apache Axis2 for Java

Last Update: November 10, 2010.

Creating a Web Service Client Application

The following walkthrough describes how to create a SOAP client application to access the NCBI Entrez Utilities Web Service using Axis2 for Java.

Preinstalled Software

You should have Apache Axis2 for Java installed on your computer, for example, in C:\SOAP\Axis2 directory. Also, you need JDK (Java Development Kit) to compile java files generated by Axis2.

There is wsdl2java.bat batch file (or wsdl2java.sh shell script file on Linux) in the bin directory of Axis2 installation. You will use it to generate Web Service stub files.

Create compile.bat file in the same bin directory:

echo off
set JAVA_HOME=<JDK_directory>
set AXIS2_HOME=<Axis2_directory>


setlocal EnableDelayedExpansion
set AXIS2_CLASS_PATH=%AXIS2_HOME%
FOR %%c in ("%AXIS2_HOME%\lib\*.jar") DO set AXIS2_CLASS_PATH=!AXIS2_CLASS_PATH!;%%c


echo Using AXIS2_HOME:   %AXIS2_HOME%
echo Using JAVA_HOME:    %JAVA_HOME%


"%JAVA_HOME%\bin\javac" -J-ms20m -J-mx1000m   -classpath %AXIS2_CLASS_PATH% src\gov\nih\nlm\ncbi\www\soap\eutils\*.java


endlocal

Create run.bat file in the same bin directory:

echo off
set JAVA_HOME=<JDK_directory>
set AXIS2_HOME=<Axis2_directory>
set JAVA_OPTS=


setlocal EnableDelayedExpansion
set AXIS2_CLASS_PATH=%AXIS2_HOME%;src\
FOR %%c in ("%AXIS2_HOME%\lib\*.jar") DO set AXIS2_CLASS_PATH=!AXIS2_CLASS_PATH!;%%c


echo Using AXIS2_HOME:   %AXIS2_HOME%
echo Using JAVA_HOME:    %JAVA_HOME%


"%JAVA_HOME%\bin\java" %JAVA_OPTS% -classpath %AXIS2_CLASS_PATH% gov.nih.nlm.ncbi.www.soap.eutils.Client


endlocal

Note: You should change AXIS2_HOME and JAVA_HOME values to actual directories where Axis2 and Java Development Kit are installed on your computer.

Note: If you connect to Internet through a proxy, the JAVA_OPTS should include proxy settings, for example:

set JAVA_OPTS= -Dhttp.proxySet=true -Dhttp.proxyHost=myproxy.mycompany.com -Dhttp.proxyPort=80

Also, you need to supply proxyUser and proxyPassword if the proxy server requires authentification. Then the set command could look like this:

set JAVA_OPTS= -Dhttp.proxySet=true -Dhttp.proxyHost=myproxy.mycompany.com -Dhttp.proxyPort=80 -Dhttp.proxyUser=user -Dhttp.proxyPassword=password

Generating Java source files

You can either generate Java source files and compile them yourself or just download them from our FTP site. The FTP directory for the current version, eUtils SOAP v2.0 + Axis2 v1.5.2 + JDK v6.0.12, is here:

ftp://ftp.ncbi.nlm.nih.gov/pub/eutils/soap/v2.0/java/axis2_1.5.2_jdk_6.0.12/

If you prefer to use pre-compiled classes, download eutils_axis2.jar file with gov.nih.nlm.ncbi.www.soap.eutils package and add it to the -classpath list in run.bat. In this case you may skip the rest of the current paragraph.

If you want to generate files from the scratch do the following:

Before running the wsdl2java.bat set JAVA_HOME and AXIS2_HOME environment parameters. Those parameters could be added to wsdl2java.bat itself, for example:

set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_12

set AXIS2_HOME=C:\SOAP\axis2-1.5.2

Also, see last note in the Preinstalled Software section.

Run the following command:

wsdl2java -uri http://eutils.ncbi.nlm.nih.gov/soap/v2.0/eutils.wsdl

Or

wsdl2java -uri <path to local file eutils.wsdl>

if you downloaded eUtils WSDL/XSD files from our FTP site.

It should generate the stub files and put them into the src\gov\nih\nlm\ncbi\www\soap\eutils directory.

Client Application Example

The example demonstrates how to make a remote call of the EInfo utility.

Create file Client.java in the gov\nih\nlm\ncbi\www\soap\eutils directory:

package gov.nih.nlm.ncbi.www.soap.eutils;
import gov.nih.nlm.ncbi.www.soap.eutils.*;


public class Client {
    public static void main(String[] args) throws Exception
    {
        // eInfo utility returns a list of available databases
        try
        {
            EUtilsServiceStub service = new EUtilsServiceStub();
           
            // call NCBI EInfo utility
            EUtilsServiceStub.EInfoRequest req = new EUtilsServiceStub.EInfoRequest();
            EUtilsServiceStub.EInfoResult res = service.run_eInfo(req);
            // results output
            for(int i=0; i<res.getDbList().getDbName().length; i++)
            {
                System.out.println(res.getDbList().getDbName()[i]);
            }
        }
        catch(Exception e) { System.out.println(e.toString()); }
    }
}    

Compiling the source code

Run compile.bat

Run Example

Run run.bat

Using Eclipse for development

To create a project with SOAP eUtils:

1.

Download eutils_axis2.jar file from our FTP site. The latest version is located here: ftp://ftp​.ncbi.nlm.nih​.gov/pub/eutils/soap/v2​.0/java/axis2_1.5.2_jdk_6.0.12/

2.

Create a new Java project in Eclipse.

3.

Open project’s Properties dialog; select Java Build Path item and switch to Libraries tab.

4.

Click Add External Jars button, select eutils_axis2.jar and click Open button.

5.

Click Add External Jars button again, navigate to axis-1.5.2/lib directory, select all files and click Open

Now you can create a new class in your project, call it Client. Now you can copy examples’ source code provided below and run the application.

NCBI Entrez Utilities Web Service API

The list of methods recommended for use with Axis2 for Java is below.

Click on parameter to get its description.

Click on method name to see the example of use.

public Result run_eGquery(EGqueryRequest parameters) throws java.rmi.RemoteException;

EGqueryRequest class properties:

public EInfoResult run_eInfo(EInfoRequest parameters) throws java.rmi.RemoteException;

EInfoRequest class properties:

public ELinkResult run_eLink(ELinkRequest parameters) throws java.rmi.RemoteException;

ELinkRequest class properties:

public EPostResult run_ePost(EPostRequest parameters) throws java.rmi.RemoteException;

EPostRequest class properties:

public ESearchResult run_eSearch(ESearchRequest parameters) throws java.rmi.RemoteException;

ESearchRequest class properties:

public ESpellResult run_eSpell(ESpellRequest parameters) throws java.rmi.RemoteException;

ESpellRequest class properties:

public ESummaryResult run_eSummary(ESummaryRequest parameters) throws java.rmi.RemoteException;

ESummaryRequest class properties:

public EFetchResult run_eFetch(EFetchRequest parameters) throws java.rmi.RemoteException;

EFetchRequest class properties:

for Molecular Biology Databases

Examples

Call EGQuery

package gov.nih.nlm.ncbi.www.soap.eutils;
import gov.nih.nlm.ncbi.www.soap.eutils.*;
public class Client
{
    public static void main(String[] args) throws Exception
    {
        // run_eGquery provides Entrez database counts for a single search
        try
        {
            EUtilsServiceStub service = new EUtilsServiceStub();
            // call NCBI eGQuery utility
            EUtilsServiceStub.EGqueryRequest req = new EUtilsServiceStub.EGqueryRequest();
            req.setTerm("mouse");
            EUtilsServiceStub.Result res = service.run_eGquery(req);
            // results output
            System.out.println("Search term: " + res.getTerm());
            System.out.println("Results: ");
            for (int i = 0; i < res.getEGQueryResult().getResultItem().length; i++)
            {
                System.out.println("  " + res.getEGQueryResult().getResultItem()[i].getDbName() +
                                   ": " + res.getEGQueryResult().getResultItem()[i].getCount());
            }
        }
        catch (Exception e) { System.out.println(e.toString()); }
    }
}

Call EInfo

package gov.nih.nlm.ncbi.www.soap.eutils;
import gov.nih.nlm.ncbi.www.soap.eutils.*;
public class Client
{
    public static void main(String[] args) throws Exception
    {
        // eInfo utility returns a list of available databases
        try
        {
            EUtilsServiceStub service = new EUtilsServiceStub();
            // call NCBI EInfo utility
            EUtilsServiceStub.EInfoRequest req = new EUtilsServiceStub.EInfoRequest();
            EUtilsServiceStub.EInfoResult res = service.run_eInfo(req);
            // results output
            for (int i = 0; i < res.getDbList().getDbName().length; i++)
            {
                System.out.println(res.getDbList().getDbName()[i]);
            }
        }
        catch (Exception e) { System.out.println(e.toString()); }
    }
}

Call EPost

package gov.nih.nlm.ncbi.www.soap.eutils;
import gov.nih.nlm.ncbi.www.soap.eutils.*;
public class Client
{
    public static void main(String[] args) throws Exception
    {
        // put ID list to history for later use
        try
        {
            EUtilsServiceStub service = new EUtilsServiceStub();
            // call NCBI EPost utility
            EUtilsServiceStub.EPostRequest req = new EUtilsServiceStub.EPostRequest();
            req.setDb("pubmed");
            req.setId("123,456,37281,983621");
            EUtilsServiceStub.EPostResult res = service.run_ePost(req);
            // results output
            System.out.println("WebEnv: " + res.getWebEnv() + "\nQueryKey: " + res.getQueryKey());
        }
        catch (Exception e) { System.out.println(e.toString()); }
    }
}

Call ESearch

package gov.nih.nlm.ncbi.www.soap.eutils;
import gov.nih.nlm.ncbi.www.soap.eutils.*;
public class Client
{
    public static void main(String[] args) throws Exception
    {
        // search in PubMed Central for stem cells in free fulltext articles
        try
        {
            EUtilsServiceStub service = new EUtilsServiceStub();
            // call NCBI ESearch utility
            // NOTE: search term should be URL encoded
            EUtilsServiceStub.ESearchRequest req = new EUtilsServiceStub.ESearchRequest();
            req.setDb("pmc");
            req.setTerm("stem+cells+AND+free+fulltext[filter]");
            req.setRetMax("15");
            EUtilsServiceStub.ESearchResult res = service.run_eSearch(req);
            // results output
            System.out.println("Original query: stem cells AND free fulltext[filter]");
            System.out.println("Found ids: " + res.getCount());
            System.out.print("First " + res.getRetMax() + " ids: ");
            for (int i = 0; i < res.getIdList().getId().length; i++)
            {
                System.out.print(res.getIdList().getId()[i] + " ");
            }
            System.out.println();
        }
        catch (Exception e) { System.out.println(e.toString()); }
    }
}

Call ESpell

package gov.nih.nlm.ncbi.www.soap.eutils;
import gov.nih.nlm.ncbi.www.soap.eutils.*;
public class Client
{
    public static void main(String[] args) throws Exception
    {
        // retrieves spelling suggestions
        try
        {
            EUtilsServiceStub service = new EUtilsServiceStub();
            // call NCBI ESpell utility
            EUtilsServiceStub.ESpellRequest req = new EUtilsServiceStub.ESpellRequest();
            req.setDb("pubmed");
            req.setTerm("mouss");
            EUtilsServiceStub.ESpellResult res = service.run_eSpell(req);
            // results output
            System.out.println("Misspelled word: " + res.getQuery());
            System.out.println("Corrected word: " + res.getCorrectedQuery());
        }
        catch (Exception e) { System.out.println(e.toString()); }
    }
}

Call ESummary

package gov.nih.nlm.ncbi.www.soap.eutils;
import gov.nih.nlm.ncbi.www.soap.eutils.*;
public class Client {
    public static void main(String[] args) throws Exception
    {
        // retrieves document Summaries by list of primary IDs
        try
        {
            EUtilsServiceStub service = new EUtilsServiceStub();
            // call NCBI ESummary utility
            EUtilsServiceStub.ESummaryRequest req = new EUtilsServiceStub.ESummaryRequest();
            req.setDb("nucleotide");
            req.setId("28864546,28800981");
            EUtilsServiceStub.ESummaryResult res = service.run_eSummary(req);
            // results output
            for(int i=0; i<res.getDocSum().length; i++)
            {
                System.out.println("ID: "+res.getDocSum()[i].getId());
                for (int k = 0; k < res.getDocSum()[i].getItem().length; k++)
                {
                    System.out.println("    " + res.getDocSum()[i].getItem()[k].getName() +
                                       ": " + res.getDocSum()[i].getItem()[k].getItemContent());
                }
            }
            System.out.println("-----------------------\n");
        }
        catch(Exception e) { System.out.println(e.toString()); }
    }
}

Call EFetch

First, generate Java classes for Taxonomy database:

wsdl2java -uri http://eutils.ncbi.nlm.nih.gov/soap/v2.0/efetch_taxon.wsdl

Each supported database has corresponding WSDL file, the full list is available on the home page.

Taxonomy database example:

package gov.nih.nlm.ncbi.www.soap.eutils;
import gov.nih.nlm.ncbi.www.soap.eutils.*;
public class Client
{
    public static void main(String[] args) throws Exception
    {
        // fetch a record from Taxonomy database
        try
        {
            EFetchTaxonServiceStub service = new EFetchTaxonServiceStub();
            // call NCBI EFetch utility
            EFetchTaxonServiceStub.EFetchRequest req = new EFetchTaxonServiceStub.EFetchRequest();
            req.setId("9685,522328");
            EFetchTaxonServiceStub.EFetchResult res = service.run_eFetch(req);
            // results output
            for (int i = 0; i < res.getTaxaSet().getTaxon().length; i++)
            {
                System.out.println(res.getTaxaSet().getTaxon()[i].getScientificName() + ": " +
                                   res.getTaxaSet().getTaxon()[i].getDivision() + " (" +
                                   res.getTaxaSet().getTaxon()[i].getRank() + ")");
            }
        }
        catch (Exception e) { System.out.println(e.toString()); }
    }
}

Using WebEnv & QueryKey example

First, generate Java classes for Pubmed database:

wsdl2java -uri http://eutils.ncbi.nlm.nih.gov/soap/v2.0/efetch_pubmed.wsdl

Each supported database has corresponding WSDL file, the full list is available on the home page.

package gov.nih.nlm.ncbi.www.soap.eutils;
import gov.nih.nlm.ncbi.www.soap.eutils.*;
public class Client
{
    public static void main(String[] args) throws Exception
    {
        String WebEnv = "";
        String query_key = "";
        // STEP #1: search in PubMed for "cat"
        //
        try
        {
            EUtilsServiceStub service = new EUtilsServiceStub();
            // call NCBI ESearch utility
            EUtilsServiceStub.ESearchRequest req = new EUtilsServiceStub.ESearchRequest();
            req.setDb("pubmed");
            req.setTerm("cat");
            req.setSort("PublicationDate");
            req.setUsehistory("y");
            EUtilsServiceStub.ESearchResult res = service.run_eSearch(req);
            // results output
            WebEnv = res.getWebEnv();
            query_key = res.getQueryKey();
            System.out.println("WebEnv: " + WebEnv + "\nQueryKey: " + query_key);
            System.out.println();
        }
        catch (Exception e) { System.out.println(e.toString()); }
        // STEP #2: fetch 5 records from pubmed starting from record #10
        //
        try
        {
            EFetchPubmedServiceStub service = new EFetchPubmedServiceStub();
            // call NCBI EFetch utility
            EFetchPubmedServiceStub.EFetchRequest req = new EFetchPubmedServiceStub.EFetchRequest();
            req.setWebEnv(WebEnv);
            req.setQuery_key(query_key);
            req.setRetstart("10");
            req.setRetmax("5");
            EFetchPubmedServiceStub.EFetchResult res = service.run_eFetch(req);
            // results output
            // PubmedArticleSet array can include articles of PubmedArticleType and
            // PubmedBookArticleType types. There should be separate display method
            // for each article's type.
            for (int i = 0; i < res.getPubmedArticleSet().getPubmedArticleSetChoice().length; i++)
            {
                  EFetchPubmedServiceStub.PubmedArticleType art = res.getPubmedArticleSet().getPubmedArticleSetChoice()[i].getPubmedArticle();
                  EFetchPubmedServiceStub.PubmedBookArticleType book = res.getPubmedArticleSet().getPubmedArticleSetChoice()[i].getPubmedBookArticle();
                  if(art!=null) {
                        System.out.println("ID (article): " + art.getMedlineCitation().getPMID());
                        System.out.println("Title: " + art.getMedlineCitation().getArticle().getArticleTitle());
                  } else if(book!=null) {
                        System.out.println("ID (book): " + book.getBookDocument().getPMID());
                        System.out.println("Title: " + book.getBookDocument().getArticleTitle());
                  }
                  System.out.println("--------------------------\n");
            }
        }
        catch (Exception e) { System.out.println(e.toString()); }
    }
}

Using xmlbeans binding

One can use xmlbeans binding instead of default as a workaround for efetch_pmc known issue or for any other purpose.

eFetch/PMC JAR file can be generated from the scratch or downloaded from our FTP site.

If you prefer to use the downloaded file then skip to Using eutils_pmc_xmlbeans.jar to fetch PMC articles

Generating Java stub files

To generate java source files run the following command:

wsdl2java –d xmlbeans -uri http://eutils.ncbi.nlm.nih.gov/soap/v2.0/eutils.wsdl

or

wsdl2java –d xmlbeans -uri <path to local file eutils.wsdl>

This command can take a long time to complete. There should be two new directories after it finishes: src and resources.

The next step is compiling the source files and creating JAR file.

Compiling the source files

To compile source files in Eclipse:

  • Select File->New->Java Project menu item

  • Click Create project from existing source radio button

  • Enter any project name.

  • Select a directory where src and resources directories mentioned above are located

  • Click Next button and switch to Libraries tab

  • Click Add External Jars button again, navigate to axis-1.5.2/lib directory, select all files and click Open

  • Click Finish

  • Select Project->Build All menu item

Eclipse should create bin directory containing gov and org directories with compiled Java classes.

Creating JAR file

Copy schemaorg_apache_xmlbeans directory from resources directory to bin directory created by Eclipse. Now there should be three directories: gov, org, and schemaorg_apache_xmlbeans. To create a JAR file run the following command:

jar.exe cvfM eutils_pmc_xmlbeans.jar org/ gov/ schemaorg_apache_xmlbeans/

Using eutils_pmc_xmlbeans.jar to fetch PMC articles

Create a new project in Eclipse as described in Using Eclipse for development section but select efetch_pmc_xmlbeans.jar in step 4.

Copy to Client.java the following source codes:

import gov.nih.nlm.ncbi.www.soap.eutils.*;
import org.apache.xmlbeans.*;
import org.apache.xmlbeans.impl.schema.*;
import gov.nih.nlm.ncbi.www.soap.eutils.efetch_pmc.*;
import gov.nih.nlm.ncbi.www.soap.eutils.efetch_pmc.ArticleDocument.Article;
import gov.nih.nlm.ncbi.www.soap.eutils.efetch_pmc.EFetchRequestDocument.EFetchRequest;
import gov.nih.nlm.ncbi.www.soap.eutils.efetch_pmc.impl.*;


public class Client
{
      public static void main(String[] args) throws Exception
      {
            // eInfo utility returns a list of available databases
            try
            {
                  EFetchPmcServiceStub service = new EFetchPmcServiceStub();
                  EFetchRequestDocument reqDoc = EFetchRequestDocument.Factory.newInstance();
                  EFetchRequest req = EFetchRequest.Factory.newInstance();
                  req.setId("100001,100002");
                  reqDoc.setEFetchRequest(req);
                  EFetchResultDocument res = service.run_eFetch(reqDoc);
                  // results output
                  Article[] arts = res.getEFetchResult().getPmcArticleset().getArticleArray();
                  for (int i = 0; i < arts.length; i++)
                  {
                        System.out.println(arts[i].getFront().getArticleMeta().getTitleGroup().getArticleTitle().toString());
                        System.out.println("-------------------------------------");
                  }
            }
            catch (Exception e) { System.out.println(e.toString()); }
      }

Copyright Notice: http://www.ncbi.nlm.nih.gov/books/about/copyright/

Cover of Entrez Programming Utilities Help
Entrez Programming Utilities Help [Internet].

Recent activity

Your browsing activity is empty.

Activity recording is turned off.

Turn recording back on

See more...