This example demonstrates how to use the Java Microprofile KAR SDK in conjunction with Microprofile based microservices. The Server provides a greeting service that responds to requests with a message.
- Java 11
- Maven 3.6+
Build the client and server applications by doing mvn package
- Launch the Server
sh -c 'export KAR_APP_PORT=8080; mvn liberty:run'
- Invoke routes using curl
(%) curl -s -X POST -H "Content-Type: text/plain" http://localhost:8080/helloText -d 'Gandalf the Grey'
Hello Gandalf the Grey
(%) curl -s -X POST -H "Content-Type: application/json" http://localhost:8080/helloJson -d '{"name": "Alan Turing"}'
{"greetings":"Hello Alan Turing"}
- Launch the Server
kar run -app hello-java -service greeter mvn liberty:run
- Run a test client
(%) kar run -app hello-java java -jar client/target/kar-hello-client-jar-with-dependencies.jar
2020/10/06 09:47:44.068160 [STDOUT] Hello Gandalf the Grey
2020/10/06 09:47:44.068176 [STDOUT] SUCCESS!
- Use the
kar
cli to invoke a route directly (the content type for request bodies defaults to application/json).
(%) kar rest -app hello-java post greeter helloJson '{"name": "Alan Turing"}'
2020/10/06 09:48:10.929784 [STDOUT] {"greetings":"Hello Alan Turing"}
Or invoke the text/plain
route with an explicit content type:
(%) kar rest -app hello-java -content_type text/plain post greeter helloText 'Gandalf the Grey'
2020/10/06 09:48:29.644326 [STDOUT] Hello Gandalf the Grey
- If the service endpoint being invoked requires more sophisticated
headers or other features not supported by the
kar rest
command, it is still possible to use curl. However, the curl command is now using KAR's REST API to make the service call via akar
sidecar.
(%) kar run -runtime_port 32123 -app hello-java curl -s -X POST -H "Content-Type: text/plain" http://localhost:32123/kar/v1/service/greeter/call/helloText -d 'Gandalf the Grey'
2020/10/06 09:49:45.300122 [STDOUT] Hello Gandalf the Grey
The server code in HelloServices.java uses standard JAX-RS annotations like @POST
,
@Path
and @Consumes
to specify the endpoints provided by the server.
The only KAR-specific detail is the use of the KAR_APP_PORT
environment variable
in the specification of the httpPort
in server.xml.
This enables the kar
sidecar to control the port that the JVM process
will use to accept incoming requests.