Introduction to Spring Boot JPA with REST using TDD

Intermediate level Java course lab

 I. Introductions

Spring Boot is an easy to framework. Here, JPA framework shall be used to store and retrieve data in a relational database. This will be coupled with an in-memory database H2 for the ease of database connectivity. This is important topic for Pivotal Spring Professional Certification for core java training in Delhi in the current IT industry.

 II. Software Tools

This lab is provided with the following tools, however any newer versions can be used for accomplishing this lab:

  • Java platform – version 8.151
  • IntelliJ IDE – version 2017.2.6
  • Maven for dependency management – version 3

These software tools are most widely used in IT industry for advance java training in Delhi and for java projects.

III. Aim

The purpose of this lab is to create a simple web application which allows a user to register their information and later retrieve it as well. We are going to achieve this through a RESTful spring boot application because it is a great library which hides a lot of webmvc integration and allows the programmer to focus on code only. This lab help students gain more knowledge to prepare for java certification in Delhi.

IV. Lab Instructions

Maven Dependencies

Maven is used here for dependency management here. It is very useful to maintain a proper code structure as well.

Add the following dependencies.

  

Swagger

This is an excellent library that allows visualisation of RESTful calls. It also provides the CURL equivalent command in case that is preferable. It is a good example of live documentation of the code as well. It is important library used for Java training in Delhi.

  1. Create a new config class with @EnableSwagger2 annotation.

On running the application now, Swagger is being rendered /swagger-ui.html endpoint.

JPA Entity

Create the User JPA entity by annotating it with @Entity as follows:

The @NonNull validations ensure that the Entity cannot be created with null values and will throw up runtime ConstraintViolationException when trying to insert a record with null values.

JPA Repository

JPA repository is very short and simple with a method for querying. We don’t need to write more code as spring takes care of it. This will create proper SQL queries behind the scenes.

 

REST Controller

This class allows us to add the REST endpoints through which we can send and retrieve user information. Again, we are using boilerplate Spring REST code peppered with Lombok annotations.

Verification

  1. Run the application either through maven or IntelliJ Application run config.
  2. Swagger UI shows the controller methods.
  3. Register a user through the UI
  4. Check the response has status code 200.
  5. Now, retrieve the saved user.
  6. Check the response.

    Codec Networks provides IT Trainings from EC Council CEH ECSA, LPT, CHFI, Network Security, Penetration Testing, ISACA, ISC2, PECB ISO 27001LA LI, Cisco Networking CCNA CCNP, Linux Administration RHCE, Prog Languages JAVA, Advanced Java, android development. We also offer B2B Industry Solutions and Services in IT | Information|Cyber Security in Delhi NCR India.

About

Codec Networks provides IT Trainings from EC Council CEH ECSA, LPT, CHFI, Network Security, Penetration Testing, ISACA, ISC2, PECB ISO 27001LA LI, Cisco Networking CCNA CCNP, Linux Administration RHCE, Prog Languages JAVA, Advanced Java, android development. We also offer B2B Industry Solutions and Services in IT | Information|Cyber Security in Delhi NCR India.

View all posts by

Leave a Reply

Your email address will not be published. Required fields are marked *