### Install Text to Speech with Gradle Source: https://github.com/watson-developer-cloud/java-sdk/blob/master/text-to-speech/README.md Add the Text to Speech service dependency to your Gradle project. ```gradle 'com.ibm.watson:text-to-speech:16.2.0' ``` -------------------------------- ### Install Text to Speech with Maven Source: https://github.com/watson-developer-cloud/java-sdk/blob/master/text-to-speech/README.md Add the Text to Speech service dependency to your Maven project. ```xml com.ibm.watson text-to-speech 16.2.0 ``` -------------------------------- ### Recognize Speech to Text using Options Model Source: https://github.com/watson-developer-cloud/java-sdk/wiki/Migration Example of how to call the `recognize()` method in the Speech to Text service using the new Options model. This pattern is consistent across most services since version 4.0.0. ```java SpeechToText service = new SpeechToText(); service.setUsernameAndPassword(, ); File audio = new File("resources/audio.wav"); RecognizeOptions options = new RecognizeOptions.Builder() .audio(audio) .contentType(RecognizeOptions.ContentType.AUDIO_WAV) .smartFormatting(true) .build(); SpeechRecognitionResults results = service.recognize(options).execute(); ``` -------------------------------- ### Visual Recognition Service: Classify Images with File Parameter (Previous) Source: https://github.com/watson-developer-cloud/java-sdk/wiki/Migration Example of the older method for classifying images in the Visual Recognition service, which used a `File` object as a parameter. ```java // Previous version with a File parameter File image = new File("image.jpg"); ClassifyImagesOptions options = new ClassifyImagesOptions.Builder() .imagesFile(image) .build(); VisualClassification result = service.classify(options).execute(); ``` -------------------------------- ### Initialize Discovery Service with Version Source: https://github.com/watson-developer-cloud/java-sdk/blob/master/README.md Instantiate the Discovery service by providing the API version. This assumes the `ibm-credentials.env` file is correctly placed for automatic authentication. ```java Discovery service = new Discovery("2023-03-31"); ``` -------------------------------- ### Configure HTTP Client with Proxy and Logging in Java Source: https://github.com/watson-developer-cloud/java-sdk/blob/master/README.md Shows how to configure the SDK's HTTP client with options such as disabling SSL verification, setting a proxy, and defining the logging level using `HttpConfigOptions`. ```java Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxyHost", 8080)); IamAuthenticator authenticator = new IamAuthenticator(apiKey); authenticator.setProxy(proxy); Discovery service = new Discovery("2023-03-31", authenticator); // setting configuration options HttpConfigOptions options = new HttpConfigOptions.Builder() .disableSslVerification(true) .proxy(proxy) .loggingLevel(HttpConfigOptions.LoggingLevel.BASIC) .build(); service.configureClient(options); ``` -------------------------------- ### New Conversation Service: Create Counterexample with Options Class Source: https://github.com/watson-developer-cloud/java-sdk/wiki/Migration Demonstrates the new pattern using an 'Options' class for creating a counterexample in the Conversation service, promoting consistency across generated services. ```java String workspaceId = "id"; String counterExampleText = "This is the new way to create a counterexample"; CreateCounterexampleOptions createOptions = new CreateCounterexampleOptions.Builder(workspaceId, counterExampleText).build(); Counterexample response = service.createCounterexample(createOptions).execute(); ``` -------------------------------- ### Set Default Headers in Java Source: https://github.com/watson-developer-cloud/java-sdk/blob/master/README.md Explains how to set default headers, such as `X-Watson-Learning-Opt-Out`, that will be included in every request made by the service instance. ```java PersonalityInsights service = new PersonalityInsights("2017-10-13", new NoAuthAuthenticator()); Map headers = new HashMap(); headers.put(WatsonHttpHeaders.X_WATSON_LEARNING_OPT_OUT, "true"); service.setDefaultHeaders(headers); // All the api calls from now on will send the default headers ``` -------------------------------- ### Initialize Assistant v2 Service Source: https://github.com/watson-developer-cloud/java-sdk/blob/master/assistant/README.md Instantiate the Assistant v2 service client with an IAM authenticator. Ensure the correct v2 import is used. ```java // make sure to use the Assistant v2 import! import com.ibm.watson.assistant.v2.Assistant; Authenticator authenticator = new IamAuthenticator(""); Assistant service = new Assistant("2019-02-28", authenticator); MessageInput input = new MessageInput.Builder() .text("Hi") .build(); MessageOptions messageOptions = new MessageOptions.Builder() .assistantId("") .sessionId("") .input(input) .build(); MessageResponse messageResponse = service.message(messageOptions).execute().getResult(); System.out.println(messageResponse); ``` -------------------------------- ### Initialize and Query Discovery v2 API Source: https://github.com/watson-developer-cloud/java-sdk/blob/master/discovery/README.md Initialize the Discovery service client with a bearer token and perform a query on a specified project. Ensure you use the correct Discovery v2 import. ```java // Make sure to use the Discovery v2 import! Authenticator authenticator = new BearerTokenAuthenticator(""); Discovery discovery = new Discovery("2019-11-22", authenticator); // Build an empty query on an existing project. String projectId = ""; QueryOptions queryOptions = new QueryOptions.Builder() .projectId(projectId) .build(); QueryResponse queryResponse = discovery.query(queryOptions).execute().getResult(); ``` -------------------------------- ### Old Conversation Service: Create Counterexample Source: https://github.com/watson-developer-cloud/java-sdk/wiki/Migration Illustrates the previous method of passing parameters directly to service methods in the Conversation service. ```java String workspaceId = "id"; String counterExampleText = "This is an old example of creating a counterexample"; ExampleResponse response = service.createCounterexample(workspaceId, counterExampleText).execute(); ``` -------------------------------- ### Initialize Assistant v1 Service Source: https://github.com/watson-developer-cloud/java-sdk/blob/master/assistant/README.md Instantiate the Assistant v1 service client using an IAM authenticator. Ensure you use the correct v1 import. ```java // make sure to use the Assistant v1 import! import com.ibm.watson.assistant.v1.Assistant; Authenticator authenticator = new IamAuthenticator(""); Assistant service = new Assistant("2019-02-28", authenticator); MessageInput input = new MessageInput(); input.setText("Hi"); MessageOptions options = new MessageOptions.Builder(workspaceId) .input(input) .build(); MessageResponse response = service.message(options).execute().getResult(); System.out.println(response); ``` -------------------------------- ### Generate SDK Artifacts Source: https://github.com/watson-developer-cloud/java-sdk/blob/master/RELEASE.md Use these Gradle commands to generate necessary artifacts locally when manually deploying to Bintray. Ensure you check out the correct release tag first. ```bash git checkout {release tag} ./gradlew shadowJar ``` -------------------------------- ### Make Asynchronous API Call with Callback in Java Source: https://github.com/watson-developer-cloud/java-sdk/blob/master/README.md Demonstrates how to make asynchronous API calls using the `enqueue()` method and handling responses or failures via a `ServiceCallback`. ```java // make API call in the background service.listEnvironments().enqueue(new ServiceCallback() { @Override public void onResponse(Response response) { System.out.println("We did it! " + response); } @Override public void onFailure(Exception e) { System.out.println("Whoops..."); } }); // continue working in the meantime! ``` -------------------------------- ### IAM Authentication with API Key (Builder) Source: https://github.com/watson-developer-cloud/java-sdk/blob/master/README.md Use this method to let the SDK manage the IAM access token lifecycle. Provide your IAM API key during initialization. ```java Authenticator authenticator = new IamAuthenticator.Builder() .apikey("") .build(); Discovery service = new Discovery("2023-03-31", authenticator); ``` -------------------------------- ### Make Synchronous API Call in Java Source: https://github.com/watson-developer-cloud/java-sdk/blob/master/README.md Illustrates the basic synchronous method for making API calls using the `execute()` method. ```java // make API call Response response = service.listEnvironments().execute(); // continue execution ``` -------------------------------- ### List Available Voices Source: https://github.com/watson-developer-cloud/java-sdk/blob/master/text-to-speech/README.md Instantiate the Text to Speech service and retrieve a list of available voices. Ensure you have your IAM API key configured. ```java Authenticator authenticator = new IamAuthenticator(""); TextToSpeech service = new TextToSpeech(authenticator); Voices voices = service.listVoices().execute().getResult(); System.out.println(voices); ``` -------------------------------- ### Specify Retrieve and Rank Service in Gradle Source: https://github.com/watson-developer-cloud/java-sdk/wiki/Changelog When using a multi-module project, specify the desired service in your Gradle dependencies. ```gradle 'com.ibm.watson.developer_cloud:retrieve-and-rank:3.4.0' ``` -------------------------------- ### Execute Java Main Class in Dockerfile Source: https://github.com/watson-developer-cloud/java-sdk/blob/master/docker/README.md Modify this line in the Dockerfile to run your main class when building the Docker image. Ensure the package name matches your project structure. ```dockerfile RUN mvn -e exec:java -Dexec.mainClass="com.ibm." ``` -------------------------------- ### Set IBM_CREDENTIALS_FILE Environment Variable Source: https://github.com/watson-developer-cloud/java-sdk/blob/master/README.md Configure the SDK to use a specific credential file by setting the `IBM_CREDENTIALS_FILE` environment variable. This takes precedence over default file locations. ```bash export IBM_CREDENTIALS_FILE="" ``` -------------------------------- ### Add Assistant Gradle Dependency Source: https://github.com/watson-developer-cloud/java-sdk/blob/master/assistant/README.md Include this dependency in your Gradle project to use the Assistant SDK. ```gradle 'com.ibm.watson:assistant:16.2.0' ``` -------------------------------- ### IAM Authentication with API Key (Deprecated Constructor) Source: https://github.com/watson-developer-cloud/java-sdk/blob/master/README.md An alternative to the builder pattern for initializing IAM authentication. The SDK will manage the access token. ```java Authenticator authenticator = new IamAuthenticator(""); Discovery service = new Discovery("2023-03-31", authenticator); ``` -------------------------------- ### Make Asynchronous API Call with RxJava in Java Source: https://github.com/watson-developer-cloud/java-sdk/blob/master/README.md Shows how to leverage RxJava for asynchronous API calls using the `reactiveRequest()` method, returning a `Single` for reactive programming. ```java // get stream with request Single> observableRequest = service.listEnvironments().reactiveRequest(); // make API call in the background observableRequest .subscribeOn(Schedulers.single()) .subscribe(response -> System.out.println("We did it with s~t~r~e~a~m~s! " + response)); // continue working in the meantime! ```