The Insider's Guide to the Java Web Developer Nanodegree - 3
Written by Nikos Vaggalis   
Monday, 31 August 2020
Article Index
The Insider's Guide to the Java Web Developer Nanodegree - 3
Demystifying Microservices
Documentation, Testing and Project

In our ongoing in-depth coverage of the upskilling program that will enhance your career prospects as a Java web developer, we move onto the third lecture in the series, "Web Services and APIs" .

But before doing that, a few words about last chapter's final project "SuperDuperDrive", a personal storage app that allows users to store files, personal notes, and website credentials for a new company in the cloud storage business.

As with every Nanodegree project, this one was challenging and offered a great opportunity to sharpen the skills gained. From the students' part it required a lot of research, that is a lot of googling around and reading the official documentation. I've even used things not covered in the class like RequestParams, Redirection, RedirectView, RedirectAttributes and Forwarding, which I nicely blended with my Controllers.

I've used jQuery UI for better error messaging, which required triggering JavaScript code within the Thymeleaf template, and Ajax calls between Thymeleaf and my Controllers in order to populate a modal window on the client side with data sent from the server side.

The JavaScript part was the most fuzzy part of the class, and especially how to go about doing the Ajax calls. Because this subject was not covered in the official lessons, there was a veil of secretiveness around it. Fortunately,after some looking around and asking questions on the Knowledge forum I've collected the basics on how to go about it and with some tweaking and customization I've managed to finally address my needs.

As far as the most tedious part of the project went, it surely was the integration testing with Selenium. It was not a fault of the project per se but the ways of Selenium;you had to map the correct hierarchy of HTML elements of the Web page tested to the Java code and battle with timeouts occurring between loading the web page and running the test code. In the end I think it was just a matter of experience. Next time it will be much easier.

In general there's no better way to learn programming or getting to know a new environment or framework than getting your hands dirty.

This hands-on attribute of the project accompanied by a lot of research did wonders on solidifying the concepts.

With that out of the way let's now focus on the third chapter of the Java Web Developer Nanodegree Program , "Web Services and APIs".

This chapter's core curriculum consists of eight lessons which we'll look at in turn.

Lesson 1 Overview

In watching the overview, it was evident that this chapter had no relation to the previous one, it's completely new material. The most important change is that now for persistence we won't be using Mybatis, but instead JPA. On the one hand this is good because you get to know yet another data access technology, which is even more prevalent under the Spring framework, but on the other you don't get to advance your knowledge in Mybatis by building on the achievements made in the last chapter.

Lesson 2 - REST APIs

After a brief introduction to REST as an architectural style for building Web applications, the lesson gets Spring-specific in getting to know a few typical annotations of the framework;@Entity, @RestController, @GetMapping.

In the "Dog REST API" exercise we build our very first RestController that returns a list of dogs from an embedded H2 in-memory database. And we do that through JPA by extending CrudRepository, which lets the framework generate data access code like findAll() and findById(id). On top of that we also extend it by adding our own queries :

@Query("select d. id, d. breed from Dog d where d. id=:id")
String findBreedById(Long id);

@Query("select d. id, d. breed from Dog d")
List<String> findAllBreed();

@Query("select d. id, d. name from Dog d")
List<String> findAllName();

Lesson 3 - GraphQL APIs

After the brief introduction to REST and JPA, we move on to converting from REST to GraphQL, and for building a GraphQL API we won't be needing Services and Controllers anymore. However, if we still want to have a REST API available, we can add those files to the application as well, so that the same endpoints as before will be available.

As everything Spring boot, integrating GraphQL is easy - just pull the graphql-spring-boot-starter dependency and you're all set to go. We'll also need graphql-java-tools, a helper library for parsing the GraphQL schema.

We start off with a simple GraphQL schema and, based on it, we add code in Queries and Mutations. A query allows for retrieving data while a mutation updates (create, update, delete) the data stored on the server.

After that we are assigned the task of transforming the Dog REST API of the last lesson to a Dog GraphQL API. To do that we once more use Spring Initializr to create the boilerplate and collect the relevant dependencies. On top of that we also add custom exception handling and test the finished product with Postman first and another tool then called Graphiql.

Theoretically the lesson was a good introduction to GraphQL and the differences from REST, while in the practical part we learnt how to actually create a GraphQL schema, make Queries and Mutators, tie all that together and finally test them out too.

As in the last lesson, the example code involved just a single table, I guess for brevity, but since we are using JPA I would had liked to see more advanced usages too, like how to handle more complex relations between tables such as one to many mappings.



Last Updated ( Monday, 31 August 2020 )