Book Image

Spring 5.0 Projects

By : Nilang Patel
Book Image

Spring 5.0 Projects

By: Nilang Patel

Overview of this book

Spring makes it easy to create RESTful applications, merge with social services, communicate with modern databases, secure your system, and make your code modular and easy to test. With the arrival of Spring Boot, developers can really focus on the code and deliver great value, with minimal contour. This book will show you how to build various projects in Spring 5.0, using its features and third party tools. We'll start by creating a web application using Spring MVC, Spring Data, the World Bank API for some statistics on different countries, and MySQL database. Moving ahead, you'll build a RESTful web services application using Spring WebFlux framework. You'll be then taken through creating a Spring Boot-based simple blog management system, which uses Elasticsearch as the data store. Then, you'll use Spring Security with the LDAP libraries for authenticating users and create a central authentication and authorization server using OAuth 2 protocol. Further, you'll understand how to create Spring Boot-based monolithic application using JHipster. Toward the end, we'll create an online book store with microservice architecture using Spring Cloud and Net?ix OSS components, and a task management system using Spring and Kotlin. By the end of the book, you'll be able to create coherent and ?exible real-time web applications using Spring Framework.
Table of Contents (13 chapters)
Title Page
About Packt
Contributors
Preface
Index

Defining the model classes


Now, let's create Java classes to model the data in the database and also the data coming from the World Bank API. Our approach is simple. We will have one Java class for each table in our database and the columns of the database will become the properties of the Java class. 

In the generated application, the java folder is missing under the main directory. We will manually create the java folder and package the  com.nilangpatel.worldgdp, which will be the root package for the application. Let's go ahead and implement the approach we decided on. But before that, let's see an interesting project called Project Lombok

Project Lombok provides annotations for generating your getters, setters, default, and overloaded constructors, and other boilerplate code. More details on how to integrate with your IDE can be found on their project website (https://projectlombok.org/).

We need to update our pom.xml to include a dependency on Project Lombok. The following are the parts of pom.xml you need to copy and add to relevant locations in the XML:

<properties>
    <java.version>1.8</java.version>
    <lombok.version>1.16.18</lombok.version>
</properties>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
    <version>${lombok.version}</version>
</dependency>

All the model classes that we are going to create next belong to the com.nilangpatel.worldgdp.model package. The model class to represent Country data is given in the following code:

@Data
@Setter
@Getter
public class Country {
    private String code;
    private String name;
    private String continent;
    private String region;
    private Double surfaceArea;
    private Short indepYear;
    private Long population;
    private Double lifeExpectancy;
    private Double gnp;
    private String localName;
    private String governmentForm;
    private String headOfState;
    private City capital;
    private String code2;
}

The City class is not created yet, let's go ahead and create it as follows: 

@Data
@Setter
@Getter
public class City {
    private Long id;
    private String name;
    private Country country;
    private String district;
    private Long population;
}

Next is to model the CountryLanguage class, which is the language spoken in a country, as follows:

@Data
@Setter
@Getter
public class CountryLanguage {
    private Country country;
    private String language;
    private String isOfficial;
    private Double percentage;
}

We also need a model class to map the GDP information obtained from the World Bank API. Let's go ahead and create a CountryGDPclass as shown in the following code:

@Data
@Setter
@Getter
public class CountryGDP { 
    private Short year;
    private Double value;
}

At this moment, everything works perfectly fine. But when you start calling getter and setter of these model classes into some other class, you may get a compilation error. This is because we need to do one more step to configure Lombok. After you defined the Maven dependency, you will see the JAR reference from IDE. Just right-click on it and select the Run As |Java Application option. Alternatively, you can execute the following command from terminal at the location where the Lombok JAR file is kept, as follows:

java -jar lombok-1.16.18.jar

Here, lombok-1.16.18.jar is the name of JAR file. You will see a separate window pop up as follows:

Select the location of your IDE by clicking on the Specify location... button. Once selected, click on the Install / Update button to install it. You will get a success message. Just restart the IDE and rebuild the project and you will see that just by defining @Setter and @Getter, the actual setters and getters are available to other classes. You are no longer required to add them explicitly.

Using Hibernate Validator to add validations

There are a few checks we need to add to our model classes so that the data being sent from the UI is not invalid. For this, we will make use of Hibernate Validator. You are required to add the Hibernate dependency as follows:

    <properties>
        <java.version>1.8</java.version>
        <lombok.version>1.16.18</lombok.version>
        <hibernate.validator.version>6.0.2.Final</hibernate.validator.version>
    </properties>    
    <dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>${hibernate.validator.version}</version>
    </dependency>

Now go back to com.nilangpatel.worldgdp.model.Country and update it with the following:

@Data public class Country {

  @NotNull @Size(max = 3, min = 3) private String code;
  @NotNull @Size(max = 52) private String name;
  @NotNull private String continent;
  @NotNull @Size(max = 26) private String region;
  @NotNull private Double surfaceArea;
  private Short indepYear;
  @NotNull private Long population;
  private Double lifeExpectancy;
  private Double gnp;
  @NotNull private String localName;
  @NotNull private String governmentForm;
  private String headOfState;
  private City capital;
  @NotNull private String code2;
}

Next is to update the com.nilangpatel.worldgdp.model.City class in a similar way, as follows:

@Data public class City {
  @NotNull private Long id;
  @NotNull @Size(max = 35) private String name;
  @NotNull @Size(max = 3, min = 3) private String countryCode;
  private Country country;
  @NotNull @Size(max = 20) private String district;
  @NotNull private Long population;
}

And finally, update com.nilangpatel.worldgdp.model.CountryLanguage class as well, as follows:

@Data
public class CountryLanguage {
  private Country country;
  @NotNull private String countryCode;
  @NotNull @Size(max = 30) private String language;
  @NotNull @Size(max = 1, min = 1) private String isOfficial;
  @NotNull private Double percentage;
}