watson-developer-cloud / java-sdk

:1st_place_medal: Java SDK to use the IBM Watson services.
http://watson-developer-cloud.github.io/java-sdk/
Apache License 2.0
590 stars 533 forks source link

[Watson Discovery Service] Exception in thread "main" java.lang.NoSuchMethodError: com.ibm.watson.developer_cloud.http.RequestBuilder.query #808

Closed christianIreland closed 6 years ago

christianIreland commented 6 years ago

Hi, I am trying to use the SDK api to consume WDS Rest Services, in particular, I am trying to create new documents in a collection I have previously configured in my bluemix account.

Below is reported a code snipped of what I am doing:

Discovery discovery = new Discovery("2017-09-01");
discovery.setEndPoint("https://gateway.watsonplatform.net/discovery/api/");
discovery.setUsernameAndPassword("xxx", "xxx");
String environmentId = "xxx";
String collectionId = "xxx";
String documentJson = "{\"field\":\"value\"}";
InputStream documentStream = new ByteArrayInputStream(documentJson.getBytes());
Builder builder= new AddDocumentOptions.Builder(environmentId,collectionId);
builder.file(documentStream);
builder.fileContentType(HttpMediaType.APPLICATION_JSON);
DocumentAccepted documentAccepted = discovery.addDocument(builder.build()).execute();
System.out.println("Document added with the documentID: " + documentAccepted.getDocumentId());

When I try to run the code I get the following error:

Exception in thread "main" java.lang.NoSuchMethodError: com.ibm.watson.developer_cloud.http.RequestBuilder.query([Ljava/lang/Object;)Lcom/ibm/watson/developer_cloud/http/RequestBuilder; at com.ibm.watson.developer_cloud.discovery.v1.Discovery.addDocument(Discovery.java:416) at com.ibm.watson.developer_cloud.retrieve_and_rank.v1.utils.DiscoveryServiceTest.uploadDocument(DiscoveryServiceTest.java:28) at com.ibm.watson.developer_cloud.retrieve_and_rank.v1.utils.DiscoveryServiceTest.main(DiscoveryServiceTest.java:34)

So no document is created in my service of course

I found a similar issue already, which apparently does not have any solution I could use.

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

JDK version: java version "1.8.0_141" java-sdk version: 3.8.0, 3.9.0, 3.9.1, 4.0.0

lpatino10 commented 6 years ago

Hi, sorry for the issue you're having.

By java-sdk version at the bottom of your issue, do you mean that you've tested this and found it doesn't work on all of those versions? I just tested it myself using version 4.0.0 of the SDK and was able to add a document, so I was hoping for a bit more information if possible so that I can try and recreate this issue.

christianIreland commented 6 years ago

Hi Thank you for your answer, yes with the code snipped I included in the version I keep getting the same error. I Have tested it with all the reported version, of course the code for 3.8.0, 3.9.0, 3.9.1 is slightly different from what I used for the version 4.0.0 because the APIs have changed.

The error message does not change it cannot find the query method.

In my environment I imported the SDK libraries using Maven. If you could make it work correctly, would you please let me know what did you do?

mkistler commented 6 years ago

I don't know what's going on here, but the exception report you included looks very suspicious.

at com.ibm.watson.developer_cloud.retrieve_and_rank.v1.utils.DiscoveryServiceTest.uploadDocument(DiscoveryServiceTest.java:28) at com.ibm.watson.developer_cloud.retrieve_and_rank.v1.utils.DiscoveryServiceTest.main(DiscoveryServiceTest.java:34)

What is suspicious is that the Discovery service seems to be using code from the DiscoveryServiceTest in retrieve_and_rank.v1.utils. Why that should ever be the case is a mystery to me, but certainly it is not so in the 4.0 version of the SDK.

Could you recreate using the 4.0 SDK and post the specific error messages from that case?

christianIreland commented 6 years ago

Hi Mike, The code snippet included in the issue is with I use with 4.0.0. I will include in this mail an image, maybe this can clarify. I have cleaned up the code a bit more so it will look a bit different from what I originally put in the issue on git.

The class called DiscoveryServiceTest is my class, so there is no test code used from the SDK packages. I do have an old eclipse project called retrieve_and_rank, I started from an old project taken from a git repository online. I added my test class in this project because it was faster for me to start testing the WDS APIs. This should explain the fact that you are confused, the packages name are similar to what you have in the SDK I guess.

So the last 2 lines in the stack trace are coming from my code, which is trying to call the SDK (second line in the stack trace) which returns the error

Exception in thread "main" java.lang.NoSuchMethodError: 
com.ibm.watson.developer_cloud.http.RequestBuilder.query([Ljava/lang/Object;)Lcom/ibm/watson/developer_cloud/http/RequestBuilder;

Looking at the source code of the class RequestBuilder, the method query is apparently missing.

christianIreland commented 6 years ago

Thank you Mike for helping me on this issue. I have now noticed that in the code example you have ran, you were pointing to a different Maven repository location.

You point to:

com.ibm.watson.developer_cloud discovery 4.0.0

while I was pointing to:

com.ibm.watson.developer_cloud java-sdk 4.0.0

Your artifact ID is discovery mine was java-sdk. Does this mean that java-sdk does not contain the correct code???

germanattanasio commented 6 years ago

@christianIreland if you look at our integration tests there should be one for in-memory JSON objects.

See https://github.com/watson-developer-cloud/java-sdk/blob/develop/discovery/src/test/java/com/ibm/watson/developer_cloud/discovery/v1/DiscoveryServiceIT.java#L759

mkistler commented 6 years ago

The dependency info I used came from the Discovery README.

https://github.com/watson-developer-cloud/java-sdk/blob/develop/discovery/README.md

Very glad we were able to pinpoint the problem in your code.

christianIreland commented 6 years ago

Hi German, thank you for your help and also thank you to your team. I am still a bit confused and I would appreciate if you could help me to understand. I noticed that the 2 libraries, as reported below, have the exact same package structure for discovery service.

I believe this one also contains the API for other services. Is the discovery artifact the one that will be officially maintained? If not will the artifact java-sdk be fixed? I am working on an important project with an important customer, it is then important to me to make sure we are using the right things.

com.ibm.watson.developer_cloud java-sdk 4.0.0 com.ibm.watson.developer_cloud discovery 4.0.0

import com.ibm.watson.developer_cloud.discovery.v1.Discovery; import com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions; import com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted; import com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions; import com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse; import com.ibm.watson.developer_cloud.http.HttpMediaType;

lpatino10 commented 6 years ago

Both artifacts should be working correctly and will be maintained. I just double-checked this morning using both dependencies and was able to add a document to a collection in both cases.

The only difference between the two artifacts is that using just the discovery version, or another service like visual-recognition, limits you to using just that service's classes, whereas using the full java-sdk dependency gives you access to every service.