Contents


Overview

Skill Level: Beginner

This tutorial will show you how to quickly create a Java application that uses Watson APIs

Ingredients

Eclipse IDE Neon

Oracle Java 8

Bluemix Account

DevOps Services Account

Step-by-step

  1. Create your Accounts

    It's been a long time I've wrote the recipe “Bluemix Watson APIs Quickstart using Java SDK”

    https://developer.ibm.com/recipes/tutorials/bluemix-watson-apis-quickstart-using-java-sdk/

    Bluemix and its APIs have changed a lot and many people asked me to upgrade that recipe, so here it is.

    The 2016 version of it.

    First, you have to sign up for a Bluemix Account

    https://new-console.ng.bluemix.net

  2. Create your Project

    The easiest way is to add a Liberty Runtime. Find in the catalog section.

    Try to add a GIT repository to the project. If you get a message like this…

    If even the button does not work, try the old bluemix experience (if it's still there) or a non-IE browser such as Firefox or Chrome.

    Hopefully, you'll get this set of steps.

     

  3. Clone the GIT repostory into Eclipse

    Open the views windows (alt+shift+Q and then Q, or window->show view->other) and type GIT

    Then click on GIT Clone and paste the GIT URL from Bluemix

    Then select the master branch, check “Import all existing eclipse projects after clone finishes” and go

    You're gonna have something like this.

  4. Add the Watson Java SDK to the Maven dependencies

    Double click on the pom.xml file and open the depedencies tab.

    Then head to

    https://github.com/watson-developer-cloud/java-sdk

    And check the latest version available at Maven

    At the time of this writing, it was

    <dependency>
    <groupId>com.ibm.watson.developer_cloud</groupId>
    <artifactId>java-sdk</artifactId>
    <version>3.5.0</version>
    </dependency>

    So add the dependency like this

    If for some reason, eclipse complains, right click on the project, choose Maven->Update Project and then check “force update of snapshots / releases”

    Then save.

     

  5. Add a Watson Service

    In Bluemix, choose “Connections” and then “Connect new” or “connect existing” (if it 's an already provisioned watson service)

    Then re-stage.

  6. Add Java Code

    Now, let's add the java code.

    If your application is running in bluemix, it's probably generating a page like this

    So let's change this application a little.

    Open the SimpleServlet.java file inside eclipse and replace it with the following code

    package wasdev.sample.servlet;

    import java.io.IOException;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import com.ibm.watson.developer_cloud.language_translator.v2.LanguageTranslator;
    import com.ibm.watson.developer_cloud.language_translator.v2.model.Language;
    import com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationResult;

    /**
    * Servlet implementation class SimpleServlet
    */
    @WebServlet(“/SimpleServlet”)
    public class SimpleServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(“text/html”);
    response.getWriter().print(translateToSpanish(“This is a test”));
    }

    private String translateToSpanish(String text){

    LanguageTranslator service = new LanguageTranslator();
    service.setUsernameAndPassword(“<USERNAME>”, “<PASSWORD>”);
    TranslationResult translationResult = service.translate(
    text, Language.ENGLISH, Language.SPANISH)
    .execute();
    return translationResult.getFirstTranslation();
    }

    }

     

    of course, you need to replace

    service.setUsernameAndPassword(“<USERNAME>”, “<PASSWORD>”);

    with the real username and password provided in the service credentials.

    Right click on the project and commit your changes.

    Then select the SimpleServlet.java and pom.xml from the “Unstaged Changes”, right click and choose “add to index”. This will tell GIT to send these changes in your commit. Add some nice message (so you can remember in the future what this change was) and click on “commit and push”

    Great, now let's see how Bluemix and DevOps deal with that.

  7. Check DevOps and your application

    Back to the Bluemix interface, go to the runtime section and then, in the “Continuous Delivery” section (right below the GIT link), click on “Configure”

     You're going to see DevOps building and deploying your project every time you commit something new into your GIT repository.

    Hopefully you're going to see a result like this

6 comments on"Bluemix Watson APIs Quickstart using Java SDK - version 2016"

  1. xFirePenguinx October 27, 2016

    Thanks for the excellent tutorial however you missed out on alot of basic details and took me a while to search for answers(by snooping around) but helped me get the just of things for the most part. Thanks!

  2. monisia-x86-64 November 03, 2016

    Thank you for this very helpful tutorial! But I guess I missed something since the result of mine did not show the “Hello World!” nor the “Esto es una prueba”. I need to recheck what I’ve missed. Do you have a tutorial using Natural Language Classifier Watson API?

Join The Discussion