The RESTEasy grpc provider supports exposing JAX-RS services to gRPC clients. See the README.md file there for a discussion of the various mechanisms and classes involved.
There are a lot of pieces to create, and this maven archetype includes a pom.xml which incorporates the steps necessary to create them.
gRPCtoJAXRS_archetype takes the GAV of an existing JAX-RS project, called the target project, and creates a new project which can generate a WAR with all of the pieces needed to interface between a gRPC client and the existing project. We will refer to the generated project as the corresponding gRPCtoJAXRS project.
To create a gRPCtoJAXRS project from an existing project, the archetype needs several pieces of information:
-
the GAV of the original project
-
the intended GAV of the gRPCtoJAXRS project
-
the package of the target JAX-RS resource class
-
root-class, the simple name of the target JAX-RS resource class [Note "-" rather than ".".]
For example,
mvn archetype:generate \
-DarchetypeGroupId=org.jboss.resteasy -DarchetypeArtifactId=gRPCtoJAXRS-archetype -DarchetypeVersion=0.0.1-SNAPSHOT \
-DgroupId=jaxrs.example -DartifactId=jaxrs.example -Dversion=0.0.1-SNAPSHOT \
-Dpackage=org.jboss.resteasy.example -Droot-class=CC1
See jaxrs.example:jaxrs.example:0.0.1-SNAPSHOT for the sample code mentioned here.
The result is a new gRPCtoJAXRS maven project named by its artifactId. Its initial contents are
-
pom.xml
-
src/main/webapp/WEB-INF/web.xml
-
a JAX-RS resource class named <root.class>_Server
-
a template for a JAX-RS test client named <root.class>_Client
Building the gRPCtoJAXRS project downloads the src/main/java contents of the target project, and builds all of the generated classes described in RESTEasy grpc provider.
The following parameters are needed:
-
resteasy.version
-
servlet.name [elaborate here]
-
root.class
For example,
mvn -Dresteasy.version=4.7.0.Final -Dservlet.name=org.jboss.resteasy.example.ExampleApp -Droot.class=CC1 clean install
The gRPCtoJAXRS project will be populated as follows
-
src/main/java will be copied from the target project
-
src/main/proto will hold the <root.class>.proto file
-
src/main/webapp/WEB-INF will hold web.xml
-
src/test/java will hold test.grpc.<root.class>_Client.java and test.grpc.<root.class>_Server.java
-
target/generated-sources/protobuf/java will hold <root.class>_proto.java, compiled from <root.class>.proto
-
target/generated-sources/protobuf/grpc-java will hold the following generated classes:
A.
<root.class>ServiceGrpc.java
(generated by the proto compiler gRPC plugin)B.
<root.class>ServiceGrpcImpl.java
(generated byorg.jboss.resteasy.grpc.JaxrsImplBaseExtender
)C.
<root.class>_JavabufTranslator.java
(generated byorg.jboss.resteasy.plugins.protobuf.JavabufTranslatorGenerator
)D.
<root.class>MessageBodyReaderWriter.java
(generated byorg.jboss.resteasy.plugins.protobuf.ReaderWriterGenerator
)
The principal output is a WAR in the target directory which can be deployed to WildFly.
The class ${root-class}_Server is a JAX-RS resource that can start up a gRPC server by calling
http://localhost:8080/jaxrs.example.grpc-0.0.1-SNAPSHOT/root/grpcserver/start
Once the gRPC server is started, the JAX-RS resources in the target project can be invoked by an appropriate gRPC client. The class ${root-class}_Client is a JUnit test class with a number of tests.
-
There are some hard coded tests like
testInt()
andtestInteger()
that verify that basic types are handled correctly. -
There is also a template method, commented out, that indicates how to test the specific methods of the JAX-RS resources. The appropriate javabuf objects need to be created and passed to the appropriate gRPC client stub method. Then the result needs to be verified.
gRPCtoJAXRS_archetype is a work in progress. More needs to be done, including:
-
Additional primitive and wrapper tests
-
More testing.
-
Probably lots of other stuff.