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
1 |
<jersey.version>2.22.1</jersey.version> |
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.
1 2 3 4 5 6 7 8 9 10 11 |
<dependencyManagement> <dependencies> <dependency> <groupId>org.glassfish.jersey</groupId> <artifactId>jersey-bom</artifactId> <version>${jersey.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> |
1 2 3 4 5 6 7 8 |
<dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> </dependency> |
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.
1 2 3 4 |
private Client client; ... client = ClientBuilder.newClient(); |
1 2 3 4 5 6 7 8 |
WebTarget target = client.target(baseUrl); Response response = target.path("courses/").request(MediaType.APPLICATION_JSON).get(Response.class); if(response.getStatus() != 200) { System.out.println(response.readEntity(String.class)); throw new RuntimeException(response.getStatus() + ": Error from Server"); } return response.readEntity(new GenericType<List<Course>>() {}); |
Create client app that calls TechCoursesRestClient.java
1 2 3 4 5 6 7 8 9 10 |
// Get the REST Bas Url from properties file String myTechCoursesbaseUrl = AppsProperties.getProperty(AppsConstants.DRUPAL_TECH_COURSES_REST_BASEURL); System.out.println( "REST endpoint base url = " + myTechCoursesbaseUrl); // Create rest client using the base url endpoint TechCoursesRestClient myTechCoursesRestClient = new TechCoursesRestClient(myTechCoursesbaseUrl); // Get all courses by making REST client call List<Course> coursesList = myTechCoursesRestClient.getAllCourses(); System.out.println(coursesList); |
Run TechCoursesApp
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()’.
Click Next. Select getAllCourses(). Click Next. Write some JUnit tests.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@Test public void testGetAllCourses_ValidInput_returnCourseList() { TechCoursesRestClient myTechCoursesRestClient = new TechCoursesRestClient(baseUrl); List<Course> coursesList = myTechCoursesRestClient.getAllCourses(); assertNotNull("coursesList must not be null", coursesList); assertFalse("coursesList must not be empty", coursesList.isEmpty()); } @Test(expected = RuntimeException.class) public void testGetAllCourses_WrongPath_returnCourseList() { TechCoursesRestClient myTechCoursesRestClient = new TechCoursesRestClient( "http://dev-my-tech-courses.pantheon.io/wrongpath"); List<Course> coursesList = myTechCoursesRestClient.getAllCourses(); } |
Right click, Run As –> JUnit Test
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.