Sponsored Advertisement

Sending a Java Object List to a Microservice with JAX-RS @POST

Keyword List: Java, JAX-RS, JSON, @POST, Jersey, List<Object>, Microservices

I am currently rebuilding NixMash Blog from the ground up as a Microservice Application with Bootique. Here's a quick look at the current project layout where we see a shared Jangles module, a PostService Microservice and a Web Client.

Today we are sending a JAX-RS @POST Request from the Web Client to the Post Microservice. For better performance we are caching Posts and Posts retrieved by Tag in the Post Microservice, so after updating a Post in the Web Client we send a @POST to the Microservice with a List of Tag objects to refresh those tag-named caches.

Microservice Endpoint

Here is the endpoint in our Post Microservice where we receive a @POST of a list of Tags. Notice we'll be passing the list as JSON.

Let's look at using the endpoint in a PostService Module @Test. The key function here is .post(Entity.json(tags));

Sending the @POST from the Web Client

Here's what it looks like sending the @POST with the List of Tags from the Web Client. Again we see the Entity.json(tags) action. Much cleaner in Jersey to perform this function than it was in earlier versions!

What the JSON Looks Like

Before concluding let's look at what the JSON looks like. First in the Web Client where we build our Entity List<Tag> Object.

At the Post Microservice Endpoint there is very little to see since Jersey on both ends handles the JSON-to-Object conversion for us and where tags is a simple ArrayList.

Finally let's look at the List<Tag> JSON if we want to test the Post Microservice Endpoint in Postman. Notice that here we use the traditional JSON syntax of "property":"value" with no Entity descriptions.

More Like This Post