The Java SDK uses the Watson Developer Cloud services, a collection of REST APIs and SDKs that use cognitive computing to solve complex problems.
- Installation
- Usage
- Getting the Service Credentials
- IBM Watson Services
- Alchemy Language
- Alchemy Vision
- Alchemy Data News
- Concept Insights
- Conversation
- Dialog
- Discovery
- Document Conversion
- Language Translation
- Language Translator
- Natural Language Classifier
- Natural Language Understanding
- Personality Insights
- Retrieve and Rank
- Speech to Text
- Text to Speech
- Tone Analyzer
- Tradeoff Analytics
- Visual Recognition
- Changes for v4.0
- Using a Proxy
- Android
- Running in Bluemix
- Default Headers
- Debug
- Eclipse and Intellij
- License
- Contributing
All the services:
<dependency>
<groupId>com.ibm.watson.developer_cloud</groupId>
<artifactId>java-sdk</artifactId>
<version>4.0.0</version>
</dependency>
Only Retrieve and Rank:
<dependency>
<groupId>com.ibm.watson.developer_cloud</groupId>
<artifactId>retrieve-and-rank</artifactId>
<version>4.0.0</version>
</dependency>
All the services:
'com.ibm.watson.developer_cloud:java-sdk:4.0.0'
Only Retrieve and Rank:
'com.ibm.watson.developer_cloud:retrieve-and-rank:4.0.0'
Only Visual Recognition:
'com.ibm.watson.developer_cloud:visual-recognition:4.0.0'
Snapshots of the development version are available in Sonatype's snapshots repository.
Add repository to your project Gradle file
allprojects {
repositories {
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}
}
And then reference the snapshot version on your app module gradle Only Speech to Text:
'com.ibm.watson.developer_cloud:speech-to-text:3.8.1-SNAPSHOT'
Download the jar with dependencies here.
Now, you are ready to see some examples.
The examples within each service assume that you already have service credentials. If not, you will have to create a service in Bluemix.
If you are running your application in Bluemix, you don't need to specify the
credentials; the library will get them for you by looking at the VCAP_SERVICES
environment variable.
You will need the username
and password
(api_key
for AlchemyAPI) credentials, and the API endpoint for each service. Service credentials are different from your Bluemix account username and password.
To get your service credentials, follow these steps:
-
Log in to Bluemix
-
Create an instance of the service:
- In the Bluemix Catalog, select the service you want to use.
- Click Create.
-
Copy your credentials:
- On the left side of the page, click Service Credentials, and then View credentials to view your service credentials.
- Copy
url
,username
andpassword
(api_key
for AlchemyAPI or Visual Recognition).
Version 4.0 focuses on the move to programmatically-generated code for many of the services. See the changelog for the details.
This version includes many breaking changes as a result of standardizing behavior across the new generated services. Full details on migration from previous versions can be found here.
The Android SDK utilizes the Java SDK while making some Android-specific additions. This repository can be found here. It depends on OkHttp and gson.
Override the configureHttpClient()
method and add the proxy using the OkHttpClient.Builder
object.
For example:
ConversationService service = new ConversationService("2017-05-26") {
@Override
protected OkHttpClient configureHttpClient() {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxyHost", 8080));
return super.configureHttpClient().newBuilder().proxy(proxy).build();
}
};
service.setUsernameAndPassword("<username>", "<password>");
WorkspaceCollectionResponse workspaces = service.listWorkspaces(null, null, null, null).execute();
System.out.println(workspaces);
For more information see: OkHTTPClient Proxy authentication how to?
When running in Bluemix, the library will automatically get the credentials from VCAP_SERVICES
.
If you have more than one plan, you can use BluemixUtils
to get the service credentials for an specific plan.
PersonalityInsights service = new PersonalityInsights();
String apiKey = BluemixUtils.getAPIKey(service.getName(), BluemixUtils.PLAN_STANDARD);
service.setApiKey(apiKey);
Default headers can be specified at any time by using the setDefaultHeaders(Map<String, String> headers)
method.
The example below sends the X-Watson-Learning-Opt-Out
header in every request preventing Watson from using the payload to improve the service.
PersonalityInsights service = new PersonalityInsights();
Map<String, String> headers = new HashMap<String, String>();
headers.put(HttpHeaders.X_WATSON_LEARNING_OPT_OUT, 1);
service.setDefaultHeaders(headers);
// All the api calls from now on will send the default headers
You can set the correct API Endpoint for your service calling setEndPoint()
.
For example, if you have the conversation service in Germany, the Endpoint may be https://gateway-fra.watsonplatform.net/conversation/api
.
You will need to call
Conversation service = new Conversation("<version-date>");
service.sentEndPoint("https://gateway-fra.watsonplatform.net/conversation/api")
Make sure you are using the service credentials and not your Bluemix account/password.
Check the API Endpoint, you may need to update the default using setEndPoint()
.
HTTP requests can be logging by adding a loggging.properties
file to your classpath.
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=FINE
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s %4$s: %5$s%n
.level=SEVERE
# HTTP Logging - Basic
com.ibm.watson.developer_cloud.util.HttpLogging.level=INFO
The configuration above will log only the URL and query parameters for each request.
For example:
Mar 30, 2017 7:31:22 PM okhttp3.internal.platform.Platform log
INFO: --> POST https://gateway.watsonplatform.net/tradeoff-analytics/api/v1/dilemmas?generate_visualization=false http/1.1 (923-byte body)
Mar 30, 2017 7:31:22 PM okhttp3.internal.platform.Platform log
INFO: <-- 200 OK https://gateway.watsonplatform.net/tradeoff-analytics/api/v1/dilemmas?generate_visualization=false (104ms, unknown-length body)
Mar 30, 2017 7:31:23 PM okhttp3.internal.platform.Platform log
INFO: --> POST https://gateway.watsonplatform.net/tradeoff-analytics/api/v1/dilemmas?generate_visualization=true http/1.1 (12398-byte body)
Mar 30, 2017 7:31:35 PM okhttp3.internal.platform.Platform log
INFO: <-- 200 OK https://gateway.watsonplatform.net/tradeoff-analytics/api/v1/dilemmas?generate_visualization=true (12311ms, unknown-length body)
Warning: The logs generated by this logger when using the level FINE
or ALL
has the potential to leak sensitive information such as "Authorization" or "Cookie" headers and the contents of request and response bodies. This data should only be logged in a controlled way or in a non-production environment.
To build and test the project you can use Gradle (version 1.x).
Gradle:
cd java-sdk
gradle jar # build jar file (build/libs/watson-developer-cloud-4.0.0.jar)
gradle test # run tests
gradle check # performs quality checks on source files and generates reports
gradle testReport # run tests and generate the aggregated test report (build/reports/allTests)
gradle codeCoverageReport # run tests and generate the code coverage report (build/reports/jacoco)
If you want to work on the code in an IDE instead of a text editor you can easily create project files with gradle:
gradle idea # Intellij IDEA
gradle eclipse # Eclipse
Find more open source projects on the IBM Github Page
This library is licensed under Apache 2.0. Full license text is available in LICENSE.
See CONTRIBUTING.md.
See CODE_OF_CONDUCT.md.
If you are having difficulties using the APIs or you have a question about the IBM Watson Services, please ask a question on dW Answers or Stack Overflow.