Wikidata-Toolkit
Wikidata-Toolkit copied to clipboard
NoSuchMethodError when following FetchOnlineDataExample
I am using exactly the code from FetchOnlineDataExample.java (from here) and nothing else:
public static void main(String[] args) {
WikibaseDataFetcher wbdf = new WikibaseDataFetcher(
BasicApiConnection.getWikidataApiConnection(),
Datamodel.SITE_WIKIDATA);
try {
EntityDocument q42 = wbdf.getEntityDocument("Q42");
System.out.println(q42);
} catch (MediaWikiApiErrorException | IOException e) {
e.printStackTrace();
}
}
And get this error:
Exception in thread "main" java.lang.NoSuchMethodError: 'okhttp3.RequestBody okhttp3.RequestBody.create(java.lang.String, okhttp3.MediaType)'
at org.wikidata.wdtk.wikibaseapi.ApiConnection.sendRequest(ApiConnection.java:396)
at org.wikidata.wdtk.wikibaseapi.ApiConnection.sendJsonRequest(ApiConnection.java:364)
at org.wikidata.wdtk.wikibaseapi.WbGetEntitiesAction.wbGetEntities(WbGetEntitiesAction.java:202)
at org.wikidata.wdtk.wikibaseapi.WbGetEntitiesAction.wbGetEntities(WbGetEntitiesAction.java:108)
at org.wikidata.wdtk.wikibaseapi.WikibaseDataFetcher.getEntityDocumentMap(WikibaseDataFetcher.java:404)
at org.wikidata.wdtk.wikibaseapi.WikibaseDataFetcher.getEntityDocuments(WikibaseDataFetcher.java:187)
at org.wikidata.wdtk.wikibaseapi.WikibaseDataFetcher.getEntityDocuments(WikibaseDataFetcher.java:156)
at org.wikidata.wdtk.wikibaseapi.WikibaseDataFetcher.getEntityDocument(WikibaseDataFetcher.java:138)
I have this in my pom.xml:
<!-- https://mvnrepository.com/artifact/org.wikidata.wdtk/wdtk-wikibaseapi -->
<dependency>
<groupId>org.wikidata.wdtk</groupId>
<artifactId>wdtk-wikibaseapi</artifactId>
<version>0.12.1</version>
</dependency>
It seems the parameters to pass to RequestBody.create() are in the wrong oder:

Could this be a conflict between WDTK's OkHTTP and your OkHTTP? Maybe two different versions of the library are in the classpath? You could perhaps find out with mvn dependency:analyze (or dependencies:analyze, I cannot remember)
Good idea @wetneb, thanks. I am not sure if the correct OkHTTP version gets used in my case. When I run mvn dependency:tree I get:
[INFO] \- org.wikidata.wdtk:wdtk-wikibaseapi:jar:0.12.1:compile
[INFO] +- org.wikidata.wdtk:wdtk-datamodel:jar:0.12.1:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.12.3:compile
[INFO] | \- org.threeten:threeten-extra:jar:1.6.0:compile
[INFO] +- org.wikidata.wdtk:wdtk-util:jar:0.12.1:compile
[INFO] +- com.squareup.okhttp3:okhttp-urlconnection:jar:3.14.9:compile
[INFO] | \- com.squareup.okhttp3:okhttp:jar:3.14.9:compile
[INFO] | \- com.squareup.okio:okio:jar:1.17.2:compile
[INFO] +- se.akerfeldt:okhttp-signpost:jar:1.1.0:compile
[INFO] | \- oauth.signpost:signpost-core:jar:1.2.1.2:compile
[INFO] \- org.apache.commons:commons-lang3:jar:3.12.0:compil
And the OkHTTP version 3.14.9 that is listed there, is also the one my IDE is pointing to as can be seen on the far right side of the popup window in the screenshot above.
However, when I run the command you suggested, mvn dependency:analyze, I get:
[WARNING] Used undeclared dependencies found:
[WARNING] com.squareup.okhttp3:okhttp:jar:3.14.9:compile
Not sure how to interpret this 🤔
In the pom.xml of this repository here it says:
https://github.com/Wikidata/Wikidata-Toolkit/blob/58d6de3d80ddb939a2234ee6cf6aded97ebd6ee3/pom.xml#L85-L86
Should I be expecting OkHTTP version 5.0.0-alpha.2 to be used instead of 3.4.9?
When I remove the wdtk-wikibaseapi dependency from my pom.xml, these two external libraries vanish:
com.squareup.okhttp3:okhttp:3.14.9
com.squareup.okhttp3:okhttp-urlconnection:3.14.9
So no other dependency of mine is using them. I also removed the squareup folder in ~/.m2 to make sure its not a caching issue. Once I add the wdtk-wikibaseapi dependency again, the same versions get added as external libraries.
The pom.xml file in this repository is for the development version of WDTK, it is normal that versions of WDTK released in the past use other versions of this dependency. So the issue probably comes from somewhere else…
My solution for now is to have a public class FixedBasicApiConnection extends BasicApiConnection that overrides sendRequest() and passes the parameters in swapped order to RequestBody.create(), then it works. If this is actually a general bug and not something local for me, I'd be happy to make a PR that swaps the parameters in ApiConnection.
I ran into the same exact problem just now. The only okhttp dependency in my tree comes from the wbtk-wikibaseapi:
[INFO] | +- org.wikidata.wdtk:wdtk-util:jar:0.13.3:compile
[INFO] | | \- org.apache.commons:commons-compress:jar:1.21:compile
[INFO] | +- com.squareup.okhttp3:okhttp-urlconnection:jar:3.14.9:compile
[INFO] | | \- com.squareup.okhttp3:okhttp:jar:3.14.9:compile
[INFO] | | \- com.squareup.okio:okio:jar:1.17.2:compile
[INFO] | \- se.akerfeldt:okhttp-signpost:jar:1.1.0:compile
[INFO] | \- oauth.signpost:signpost-core:jar:1.2.1.2:compile
Ok, so in OpenRefine it works, and there we manually pin to okhttp 4.9.3. So let me just publish a new minor release with that dependency. We can then investigate why this is not even picked up at compilation time or in the tests. Very weird!
Ok, we actually pull in two different versions of OkHTTP as dependencies of WDTK at the moment, hence the mess. Let me clean that up.
What's the status of this bug? I guess it's related to reverting 536?
How about merging #698? It looks like it contains a fix. If not, there's still the exclusions workaround I mentioned in #694. Downgrading to stable okhttp requires a bit more work.
I'm kind of confused what I would need to exclude. I'm only using the wdtk-wikibaseapi. Not sure where okhttp 5 would be coming from...
<dependency>
<groupId>org.wikidata.wdtk</groupId>
<artifactId>wdtk-wikibaseapi</artifactId>
<version>0.13.3</version>
</dependency>
Try this (untested):
<dependency>
<groupId>org.wikidata.wdtk</groupId>
<artifactId>wdtk-wikibaseapi</artifactId>
<version>0.13.3</version>
<exclusions>
<exclusion>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</exclusion>
</exclusions>
</dependency>
If you have other dependencies that pull in okhttp, you need to do the same for them.
I tried this, but this just caused my project to miss okhttpClient. I think the quickest fix is to switch the RequestBody.create(.. arguments on line 447 of ApiConnection, though I do wonder why this does not cause a problem for everyone, as ApiConnection imports okhttp3.RequestBody The FixedBasicApiConnection workaround works, but feels very hacky.
Fine with me to try upgrading OkHTTP (but I am not sure why you think downgrading to stable would be more work - is there anything else to do beyond #694?)
@Lhaaits I will have to test this on my own app
@wetneb Mockwebserver alternative is a lot of work. Merging #694 makes it harder, because junit5 upgrade would have to be reapplied after it. I had to give my attention to two other opensource projects, so I am only getting around to this now.
As you prefer! Let me merge #698 and make a patch release so that people can test it more easily.
hey @Lhaaits, we just released a new version which could potentially solve this, can you check on your side if it works for you?
0.13.4? Still same exception:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.wikidata.wdtk.wikibaseapi.ApiConnection.sendRequest(ApiConnection.java:447)
The following method did not exist:
'okhttp3.RequestBody okhttp3.RequestBody.create(java.lang.String, okhttp3.MediaType)'
The calling method's class, org.wikidata.wdtk.wikibaseapi.ApiConnection, was loaded from the following location:
jar:file:/Users/admin/.m2/repository/org/wikidata/wdtk/wdtk-wikibaseapi/0.13.4/wdtk-wikibaseapi-0.13.4.jar!/org/wikidata/wdtk/wikibaseapi/ApiConnection.class
Thanks!
@robertvazan my offer to downgrade to stable okhttp still stands. I understand you do not like it because it downgrades Junit but it feels pretty important to me to solve this quickly. I will personally not have the time to embark on a big change of our testing architecture (by adding a dockerized Wikibase or by developing our own Wikibase-like server).
@wetneb I cannot work on this immediately, because I have one other OSS project still in need of my attention. I might need a week or more to get this done. If the quick fix I offered before does not work and the issue is urgent, feel free to downgrade okhttp for now. If I find a way to do it better by replacing mockwebserver, I will begin my PR with reapplication of junit upgrade, so that the junit5 work is not lost.
That sounds like a great plan!
@Lhaaits how about 0.13.5?
Still the same problem :/
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< nl.kb.kbga:emma >---------------------------
[INFO] Building EMMA 0.0.4-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:3.2.0:tree (default-cli) @ emma ---
[INFO] nl.kb.kbga:emma:jar:0.0.4-SNAPSHOT
[INFO] +- nl.kbif.leibniz.commons:leibniz-auth:jar:5.0.0:compile
[INFO] | +- nl.kbif.leibniz.commons:leibniz-spring:jar:5.0.0:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-validation:jar:2.6.6:compile
[INFO] | | | \- org.hibernate.validator:hibernate-validator:jar:6.2.3.Final:compile
[INFO] | | | +- jakarta.validation:jakarta.validation-api:jar:2.0.2:compile
[INFO] | | | +- org.jboss.logging:jboss-logging:jar:3.4.3.Final:compile
[INFO] | | | \- com.fasterxml:classmate:jar:1.5.1:compile
[INFO] | | \- org.springframework.boot:spring-boot-actuator:jar:2.6.6:compile
[INFO] | \- org.springframework.boot:spring-boot-starter-security:jar:2.6.6:compile
[INFO] | +- org.springframework:spring-aop:jar:5.3.18:compile
[INFO] | +- org.springframework.security:spring-security-config:jar:5.6.2:compile
[INFO] | | \- org.springframework.security:spring-security-core:jar:5.6.2:compile
[INFO] | \- org.springframework.security:spring-security-web:jar:5.6.2:compile
[INFO] +- nl.kbif.leibniz.commons:leibniz-commons:jar:5.0.0:compile
[INFO] | +- org.springframework:spring-web:jar:5.3.18:compile
[INFO] | | \- org.springframework:spring-beans:jar:5.3.18:compile
[INFO] | +- org.springframework:spring-jdbc:jar:5.3.18:compile
[INFO] | | \- org.springframework:spring-tx:jar:5.3.18:compile
[INFO] | +- com.fasterxml.jackson.dataformat:jackson-dataformat-xml:jar:2.13.2:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-core:jar:2.13.2:compile
[INFO] | | +- org.codehaus.woodstox:stax2-api:jar:4.2.1:compile
[INFO] | | \- com.fasterxml.woodstox:woodstox-core:jar:6.2.7:compile
[INFO] | +- org.slf4j:slf4j-api:jar:1.7.36:compile
[INFO] | +- org.apache.commons:commons-lang3:jar:3.12.0:compile
[INFO] | \- net.sf.saxon:Saxon-HE:jar:10.3:compile
[INFO] +- org.liquibase:liquibase-core:jar:4.11.0:runtime
[INFO] | \- javax.xml.bind:jaxb-api:jar:2.3.1:runtime
[INFO] | \- javax.activation:javax.activation-api:jar:1.2.0:runtime
[INFO] +- org.postgresql:postgresql:jar:42.3.6:runtime
[INFO] | \- org.checkerframework:checker-qual:jar:3.5.0:runtime
[INFO] +- org.projectlombok:lombok:jar:1.18.24:provided
[INFO] +- org.springframework.boot:spring-boot-configuration-processor:jar:2.7.0:compile (optional)
[INFO] +- org.springframework.boot:spring-boot-starter-actuator:jar:2.7.0:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.6.6:compile
[INFO] | | +- org.springframework.boot:spring-boot:jar:2.6.6:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.6.6:compile
[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.11:compile
[INFO] | | | | \- ch.qos.logback:logback-core:jar:1.2.11:compile
[INFO] | | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.17.2:compile
[INFO] | | | | \- org.apache.logging.log4j:log4j-api:jar:2.17.2:compile
[INFO] | | | \- org.slf4j:jul-to-slf4j:jar:1.7.36:compile
[INFO] | | +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
[INFO] | | \- org.yaml:snakeyaml:jar:1.29:compile
[INFO] | +- org.springframework.boot:spring-boot-actuator-autoconfigure:jar:2.6.6:compile
[INFO] | | \- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.13.2:compile
[INFO] | \- io.micrometer:micrometer-core:jar:1.8.4:compile
[INFO] | +- org.hdrhistogram:HdrHistogram:jar:2.1.12:compile
[INFO] | \- org.latencyutils:LatencyUtils:jar:2.0.3:runtime
[INFO] +- org.springframework.boot:spring-boot-starter-jdbc:jar:2.7.0:compile
[INFO] | \- com.zaxxer:HikariCP:jar:4.0.3:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.7.0:test
[INFO] | +- org.springframework.boot:spring-boot-test:jar:2.6.6:test
[INFO] | +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.6.6:test
[INFO] | +- com.jayway.jsonpath:json-path:jar:2.6.0:test
[INFO] | | \- net.minidev:json-smart:jar:2.4.8:test
[INFO] | | \- net.minidev:accessors-smart:jar:2.4.8:test
[INFO] | | \- org.ow2.asm:asm:jar:9.1:test
[INFO] | +- jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.3:test
[INFO] | | \- jakarta.activation:jakarta.activation-api:jar:1.2.2:test
[INFO] | +- org.assertj:assertj-core:jar:3.21.0:test
[INFO] | +- org.hamcrest:hamcrest:jar:2.2:test
[INFO] | +- org.junit.jupiter:junit-jupiter:jar:5.8.2:test
[INFO] | | +- org.junit.jupiter:junit-jupiter-api:jar:5.8.2:test
[INFO] | | | +- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO] | | | +- org.junit.platform:junit-platform-commons:jar:1.8.2:test
[INFO] | | | \- org.apiguardian:apiguardian-api:jar:1.1.2:test
[INFO] | | +- org.junit.jupiter:junit-jupiter-params:jar:5.8.2:test
[INFO] | | \- org.junit.jupiter:junit-jupiter-engine:jar:5.8.2:test
[INFO] | | \- org.junit.platform:junit-platform-engine:jar:1.8.2:test
[INFO] | +- org.mockito:mockito-core:jar:4.0.0:test
[INFO] | | +- net.bytebuddy:byte-buddy:jar:1.11.22:test
[INFO] | | +- net.bytebuddy:byte-buddy-agent:jar:1.11.22:test
[INFO] | | \- org.objenesis:objenesis:jar:3.2:test
[INFO] | +- org.mockito:mockito-junit-jupiter:jar:4.0.0:test
[INFO] | +- org.skyscreamer:jsonassert:jar:1.5.0:test
[INFO] | | \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] | +- org.springframework:spring-core:jar:5.3.18:compile
[INFO] | | \- org.springframework:spring-jcl:jar:5.3.18:compile
[INFO] | +- org.springframework:spring-test:jar:5.3.18:test
[INFO] | \- org.xmlunit:xmlunit-core:jar:2.8.4:test
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.7.0:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:2.6.6:compile
[INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.13.2:compile
[INFO] | | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.13.2:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.6.6:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.60:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.60:compile
[INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.60:compile
[INFO] | \- org.springframework:spring-webmvc:jar:5.3.18:compile
[INFO] | +- org.springframework:spring-context:jar:5.3.18:compile
[INFO] | \- org.springframework:spring-expression:jar:5.3.18:compile
[INFO] +- org.springframework.cloud:spring-cloud-config-client:jar:3.1.3:compile
[INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.6.6:compile
[INFO] | +- org.springframework.cloud:spring-cloud-commons:jar:3.1.2:compile
[INFO] | | \- org.springframework.security:spring-security-crypto:jar:5.6.2:compile
[INFO] | +- org.springframework.cloud:spring-cloud-context:jar:3.1.2:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.13.2:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.13.2.2:compile
[INFO] | \- org.apache.httpcomponents:httpclient:jar:4.5.13:compile
[INFO] | +- org.apache.httpcomponents:httpcore:jar:4.4.15:compile
[INFO] | \- commons-codec:commons-codec:jar:1.15:compile
[INFO] +- org.springframework.cloud:spring-cloud-starter-bootstrap:jar:3.1.3:compile
[INFO] | \- org.springframework.cloud:spring-cloud-starter:jar:3.1.2:compile
[INFO] | \- org.springframework.security:spring-security-rsa:jar:1.0.10.RELEASE:compile
[INFO] | \- org.bouncycastle:bcpkix-jdk15on:jar:1.68:compile
[INFO] | \- org.bouncycastle:bcprov-jdk15on:jar:1.68:compile
[INFO] +- org.testcontainers:postgresql:jar:1.17.2:test
[INFO] | \- org.testcontainers:jdbc:jar:1.17.2:test
[INFO] | \- org.testcontainers:database-commons:jar:1.17.2:test
[INFO] | \- org.testcontainers:testcontainers:jar:1.17.2:test
[INFO] | +- junit:junit:jar:4.13.2:test
[INFO] | | \- org.hamcrest:hamcrest-core:jar:2.2:test
[INFO] | +- org.rnorth.duct-tape:duct-tape:jar:1.0.8:test
[INFO] | | \- org.jetbrains:annotations:jar:17.0.0:test
[INFO] | +- com.github.docker-java:docker-java-api:jar:3.2.13:test
[INFO] | \- com.github.docker-java:docker-java-transport-zerodep:jar:3.2.13:test
[INFO] | +- com.github.docker-java:docker-java-transport:jar:3.2.13:test
[INFO] | \- net.java.dev.jna:jna:jar:5.8.0:test
[INFO] \- org.wikidata.wdtk:wdtk-wikibaseapi:jar:0.13.5:compile
[INFO] +- org.wikidata.wdtk:wdtk-datamodel:jar:0.13.5:compile
[INFO] | \- org.threeten:threeten-extra:jar:1.7.0:compile
[INFO] +- org.wikidata.wdtk:wdtk-util:jar:0.13.5:compile
[INFO] | \- org.apache.commons:commons-compress:jar:1.21:compile
[INFO] +- com.squareup.okhttp3:okhttp-urlconnection:jar:3.14.9:compile
[INFO] | \- com.squareup.okhttp3:okhttp:jar:3.14.9:compile
[INFO] | \- com.squareup.okio:okio:jar:1.17.2:compile
[INFO] \- se.akerfeldt:okhttp-signpost:jar:1.1.0:compile
[INFO] \- oauth.signpost:signpost-core:jar:1.2.1.2:compile
[INFO] ------------------------------------------------------------------------
3.14.9 is the only version of okhttp being imported. Still not sure why RequestBody.create in BasicApiConnection is expecting java.lang.String, okhttp3.MediaType
Hmmm, I am not sure where "3.14.9" is coming from. This new version of WDTK depends on OkHTTP 4.2.2. Could you try adding an explicit dependency to com.squareup.okhttp3:okhttp-urlconnection:4.2.2 in your POM?

Still uses okhttp:3.14.9, and still gives the error. I'll also try to get it to work locally and maybe make a pull request. I also tried 'invalidate cache' in IntelliJ and mvn clean install -U, but no luck...
@Lhaaits Did you remove the exclusion rule from your POM?