Java REST Client to access Drupal Content – Part 2

In Part 1 we have created a REST API which provides content stored in Drupal. We will now write a Java REST client to access the content. We will use Jersey which is the JAX-RS API (JSR 311 & JSR 339) reference implementation.

We will begin by creating a Java standalone project as described here (or use an existing project if already created).

Maven pom.xml updates

We will use Jersey BOM (Bill Of Materials). This helps to avoid version mismatches among jersey dependencies. Once BOM is added, there is no need to add version attribute in Jersey dependencies.

Java REST Client

We will write the jersey client calls in a separate class, so that any client can use this like a SDK. It is a common practice for the service provider to give a client SDK, which helps as a way of enforcing contract.

Create client app that calls TechCoursesRestClient.java

Run TechCoursesApp

image

JUnit test cases

Select TechCoursesRestClientTest.java in Eclipse. Right click –> New –> Other –> JUnit Test Case. Make sure ‘New JUnit 4 test’ is selected. Change source folder to standalone-utils/src/test/java. Check the option to create Stub for ‘setupBeforeClass()’.

image

Click Next. Select getAllCourses(). Click Next. Write some JUnit tests.

Right click, Run As –> JUnit Test

image

Source Code on GitHub

The source code can be found on github repository at this link. A practical tutorial on how this is added to github is given in this post.

Leave a Reply

Your email address will not be published.