wso2 / api-manager

All issues, tasks, improvements and new features of WSO2 API Manager
Apache License 2.0
34 stars 8 forks source link

the java sdk can not working #2224

Open sendreams opened 1 year ago

sendreams commented 1 year ago

Description

use java sdk to invoke api can not be succeed.

Steps to Reproduce

  1. publish a api and use devportal test is ok.
  2. download the java sdk
  3. use maven to build the source code to jar
  4. make a test class to invoke the api ` @Test public void testFactoryApi() { ApiClient defaultClient = Configuration.getDefaultApiClient(); defaultClient.setBasePath("https://localhost:8243/factory/gis/1.0"); defaultClient.addDefaultHeader("Accept", "application/json");

    // Configure OAuth2 access token for authorization: default
    OAuth aa = (OAuth) defaultClient.getAuthentication("default");
    aa.setAccessToken("i_have_use_real_token_here");   // 
    DefaultApi apiInstance = new DefaultApi(defaultClient);
    Object body = new QueryIndoor(); // Object |
    try {
        apiInstance.post(body);
    } catch (ApiException e) {
        System.err.println("Exception when calling DefaultApi#post");
        System.err.println("Status code: " + e.getCode());
        System.err.println("Reason: " + e.getResponseBody());
        System.err.println("Response headers: " + e.getResponseHeaders());
        e.printStackTrace();
    }

    }`

first error is throws: java.lang.NoSuchMethodError: okhttp3.RequestBody.create(Ljava/lang/String;Lokhttp3/MediaType;)Lokhttp3/RequestBody; at org.wso2.client.api.ApiClient.serialize(ApiClient.java:977) at org.wso2.client.api.ApiClient.buildRequest(ApiClient.java:1226) at org.wso2.client.api.ApiClient.buildCall(ApiClient.java:1179)

change the source return RequestBody.create(content, MediaType.parse(contentType)); to return RequestBody.create(MediaType.parse(contentType),content);

second error throws: Exception when calling DefaultApi#post Status code: 0 Reason: null Response headers: null org.wso2.client.api.ApiException: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at org.wso2.client.api.ApiClient.execute(ApiClient.java:1074) at org.wso2.client.api.ApiClient.execute(ApiClient.java:1054) at org.wso2.client.api.factory_gis.DefaultApi.postWithHttpInfo(DefaultApi.java:175) at org.wso2.client.api.factory_gis.DefaultApi.post(DefaultApi.java:158) at com.ware4u.studio3d.plugin.xh.WSO2Tests.testFactoryApi(WSO2Tests.java:25) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63) at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.ParentRunner.run(ParentRunner.java:413) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38) at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55) Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.ssl.Alert.createSSLException(Alert.java:131) at sun.security.ssl.TransportContext.fatal(TransportContext.java:370) at sun.security.ssl.TransportContext.fatal(TransportContext.java:313) at sun.security.ssl.TransportContext.fatal(TransportContext.java:308)

Affected Component

APIM

Version

4.2.0

Environment Details (with versions)

windows server 2022 idea 2023.2.3 jdk 1.8.0_361

Relevant Log Output

No response

Related Issues

No response

Suggested Labels

java sdk

sendreams commented 1 year ago

In addition, I think this method is not convenient to use in the Java environment. One API will generate one jar file. So if I want to use multiple APIs, do I need to add multiple jars? Obviously there is too much duplicate code. I only need a regular calling method, calling the api to return the result, and I can parse the result myself. Sometimes, I just want to register the api and let wso2 solve the complex parameter configuration, and I can parse the call result myself.

sendreams commented 1 year ago

I hope you can provide more simplified java call-side code, juse call api and return raw content.