Lab 08

Spring server with JPA

Spring server

Use https://start.spring.io (Spring initializer) to create a new maven web server project.

When creating, select:

  • Maven
  • verzi 3.4.4
  • Group, artifact, name, package name - zvolte nějaké smysluplné hodnoty (ne demo a example)
  • Packaging JAR
  • Java version 21
  • Values important for successful test execution
    • Group: cz.vsb.fei
    • Artifact: java2-lab08
    • Name: java2-lab08
    • Package name: cz.vsb.fei.java2.lab08 (not an underscore but a period, otherwise they won't pass the Kelvin tests)

Add to the dependencies (using the "ADD DEPENDENCIES..." button)

  • Lombok
  • Spring Web
  • Thymeleaf
  • Spring Data JPA
  • H2 Database

Generate (download) the project and add the dependencies to pom.xml:

        <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<scope>provided</scope>
<version>6.6.11.Final</version>
</dependency>

In pom.xml add into /project/build/plugins/plugin(maven-compiler-plugin)/configuration/annotationProcessorPaths/:

                        <path>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>6.6.11.Final</version>
</path>

In pom.xml add into /project/build/plugins/plugin(maven-compiler-plugin)/:

                <executions>
<execution>
<id>process</id>
<phase>generate-sources</phase>
</execution>
</executions>

In pom.xml add into /project/build/plugins/:

            <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/annotations</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>

Implements

  • Create a Player entity with first name, last name and date of birth (firstName, lastName, dayOfBirth)
  • Create a JPA repository for Player
  • Create a REST controller with methods for CRUD operations (follow REST api rules) and for generating multiple Player objects.
  • Create a custom method using the @Query annotation and some URL for it to call (e.g. for the youngest or oldest players)
  • Create a CustomPlayerRepository class with a custom method and some URL to call for it.

Run the project as a regular java application and test if the text you choose appears at http://localhost:8080.

If you want to test Swagger UI just add a dependency:

        <dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.8.6</version>
</dependency>

Method of submission

Submit the homework solution to the Kelvin system as the "Java 2 - Homework 1 - Maven" assignment. Upload the entire contents of the src folder and the pom.xml (and run script) file to the system. Follow the attached video below.

The project will then be compiled and Unit Tests will be performed. Due to the nature of the project and the provisional testing use of the Kelvin system in the Java 2 course, do not despair in case of failure.

This is an auxiliary test, everything will still be evaluated manually. It is important to upload the files so that everything can be evaluated and analyzed for plagiarism. I believe this is a formality and all of you are creating your own code.