问题
Not much written about using Google Cloud endpoints from a Java client. There are hints that it can be done in various documents but very little written about it. I've been able to get it work but there are some questions.
First question: Is there some documentation or an example about how to use Google Cloud Endpoint from a Java client?
I'm using the GPE to generate an endpoints client library. The output is the creation of endpoint-libs directory with a bunch of stuff. Documentation says, "The generated service-specific library can be found in the top level directory of the zip file: google-api-services-mayapp-v1-rev20140417180959-1.16.0-rc.jar". With my version of the GPE (3.5.1) there is no such JAR file created.
This video of the tic-tac-toe example (go to the 23 min mark) shows some source files being copied from the endpoint-libs directory into an Android client app.
https://www.youtube.com/watch?v=NU_wNR_UUn4
Second question: Is the documentation wrong regarding the creating of the above mentioned JAR file? Is there any documentation about the generated sources files and how to use them? Perhaps there is some documentation in regards to building an Android app, but I don't know Android. In any case, I sifted through some of the Android client code.
The video goes on to show a "service" object being used to reference the endpoints. I've adapted the code to work with a Java client.
MyApp.Builder builder = new MyApp.Builder( new NetHttpTransport(), new GsonFactory(), null );
service = builder.build();
Method1 method1 = service.getEndpointMethod1();
method1.execute();
The Android sample uses AndroidHttp.newCompatibleTransport() to create an HttpTransport as the first argument to MyApp.Builder();
Third question: Is the method I used for creating an HttpTransport proper for a Java client?
Fourth question: How much of the Android client documentation applies to use of an endpoint from a Java client - since they are both Java clients? What wouldn't apply?
回答1:
(OP: see "Without Maven" below, since you say you're not using Maven)
With Maven
My Cloud Endpoints project pom.xml include the plugin config:
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>${appengine.version}</version>
<configuration>
<enableJarClasses>false</enableJarClasses>
<version>${app.version}</version>
</configuration>
<executions>
<execution>
<goals>
<!-- Generates the discovery document and client library
during the compile phase. -->
<goal>endpoints_get_discovery_doc</goal>
<goal>endpoints_get_client_lib</goal>
</goals>
</execution>
</executions>
</plugin>
Note the <goal>endpoints_get_client_lib</goal>
entry, which was not included in the original skeleton, and is the important piece here.
For me, this produces and installs a jar named {myApiName}-v1-1.21.0-SNAPSHOT.jar
. See "Results" below for what I see in the .jar.
Without Maven
Under the hood, Maven seems to be using one of the mechanisms that alex@ pointed to, endpoints.sh
(docs). When I run "mvn install" on my Endpoints module, I see a message like
Executing endpoints Command=[get-client-lib,
-cp, {lots of .jar files}, -o, {path to output WEB-INF dir},
-w, {path to generated file working dir?}, -l, java,
-bs, maven, com.example.MyApi]
I'm guessing that this is a list of the args that it's passing to endpoints.sh
, though I haven't checked.
Results
The resulting .jar file looks like this, with the top-level client MyApiName class, some supporting classes, and model classes generated from the discovery document:
META-INF/MANIFEST.MF
META-INF/maven/com.example/myApiName/pom.properties
META-INF/maven/com.example/myApiName/pom.xml
com/example/myApiName/MyApi$AddBatch.class
com/example/myApiName/MyApi$Builder.class
com/example/myApiName/MyApi.class
com/example/myApiName/MyApiRequest.class
com/example/myApiName/MyApiRequestInitializer.class
com/example/myApiName/MyApiScopes.class
com/example/myApiName/model/Thing.class
com/example/myApiName/model/OtherThing.class
来源:https://stackoverflow.com/questions/23177772/generating-google-cloud-endpoints-for-java-client