0% found this document useful (0 votes)
28 views

Spring Task

Uploaded by

sklencarovamaria
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Spring Task

Uploaded by

sklencarovamaria
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Spring task

Create a project (https://start.spring.io/) based on Spring Boot using spring-boot-starter-


web starter and lombok library. Then create a class DummyLogger in the project, which
will log at the start of the application with the information Hello from task1.

Exercise 1 - solution

Exercise 2
Create a project (https://start.spring.io/) based on Spring Boot using:
• spring-boot-starter-web
• lombok
Inject the DummyLogger class into:
• the CommandLineRunnerWithConstructorInjection class using a constructor
• the CommandLineRunnerWithFieldInjection class directly to the field
• the CommandLineRunnerWithSetterInjection class using a setter
Base your implementations on the following classes / class frameworks:

1
Exercise 2 - solution

2
Exercise 3
Create a project (https://start.spring.io/) based on Spring Boot using:
• spring-boot-starter-web
• lombok

Create two beans implementing the DummyLogger interface. In implementations of


the sayHello method, let them print any String to the screen. In addition, let one of the
implementations be marked as the main bean.
Create beans that are implementations of the CommandLineRunner interface, which:
• inject the main implementation of the DummyLogger interface and calls
the sayHello method at application startup
• inject the second implementation of the DummyLogger interface and calls
the sayHello method at application startup
• inject all implementations of the DummyLogger interface and calls
the sayHello method on both implementations at startup
Exercise 3 - exercise

3
Exercise 4
Create a project (https://start.spring.io/) using Spring Boot using:
• spring-boot-starter-web
• lombok
Create 3 beans in a class named UtilConfiguration:
• a bean named dummyLogger which is an implementation of the DummyLogger class
• a bean named listUtility implementing the ListUtil class. Use the method name to
establish the correct bean name.
• bean named stringUtility which is an implementation of the StringUtil class. The
method creating this bean should be named stringUtil.

4
Exercise 4 - solution

Exercise 5
Create a project (https://start.spring.io/) based on Spring Boot using:
• spring-boot-starter-web
• lombok
Inject the following scaffold definition of the WelcomeMessageLogger class using the @
Value annotation, values, which you will define in the file application.properties using the
following keys:
• pl.sdacademy.welcome.text.value
• pl.sdacademy.welcome.text.enable
The property pl.sdacademy.welcome.text.value defines the text that should be printed at
application startup. The default should be none.
The property pl.sdacademy.welcome.text.enable defines whether the text defined
in pl.sdacademy.welcome.text.value should be displayed on the screen. There is no
default value.

5
Inject both properties using the constructor.

Exercise 5 - solution

Exercise 6
Create a project (https://start.spring.io/) based on Spring Boot using:
• spring-boot-starter-web
• lombok
• spring-boot-starter-validation
On the basis of the following properties from the application.properties file, create a
configuration class which represents a property group.

6
These properties should be ** validated ** at the start of the application and meet the
following conditions:
• the pl.sdacademy.zad6.email field is required and must be a valid email address
• the pl.sdacademy.zad6.age field is required and must be at least 18
• the pl.sdacademy.zad6.last-name field is required and must be between 3 and 20
characters
• the pl.sdacademy.zad6.address field is required and consists of 2 parts separated by
a space
• the pl.sdacademy.zad6.values collection is not empty
• the pl.sdacademy.zad6.custom-attributes collection is not empty
Exercise 6 - solution

Exercise 7
Create a project (https://start.spring.io/) based on Spring Boot using:
7
• spring-boot-starter-web
• lombok
Create an endpoint GET under the URI /api/pairs/{name}that will return the following object
(as JSON):

For this object, the value of the name field should come from the variable part of the path,
and the pairs field should always be an empty map. This object should be serialized as
follows:
• in the dev profile:
fields with the value null are not serialized, but empty collections do
field naming follows the snake_case convention
• in the production profile:
null fields and empty queues are not serialized
field naming uses the camelCase convention
Confirm correct behavior by running the application in the appropriate profile and sending
the appropriate HTTP request.
Exercise 7 - solution

8
Exercise 8
Create a project (https://start.spring.io/) based on Spring Boot using:
• spring-boot-starter-web
• lombok
Implement a controller (REST) with an endpoint GET/api/random-boolean that returns a
value using the RandomBooleanProvider bean and the getValue method. When making
several HTTP requests in a row, let endpoint return a different value.
Do this task without modifying the body of the RandomBooleanProvider class.

9
Exercise 8 - solution

Exercise 9
Create a project (https://start.spring.io/) based on Spring Boot using:
• spring-boot-starter-web
• spring-boot-starter-data-jpa
• lombok
• h2
Part 1:
In the class FileData define the entity whose contents will be kept in the table files_data.
The entity ID is an automatically generated UUID object. In addition, this entity has the
following columns:
• file_name
• extension
• size_in_kb
• content (assume that the file content is small)
This entity should be created at application startup and you should be able to confirm it in
the h2 console at http://localhost:8080/h2, using the following URL: jdbc:h2:mem:testdb,
user test with password password.
Create a repository for this entity, thanks to which you will perform basic CRUD operations.

10
Part 2
Create a REST API that will provide the following endpoints:
• GET /api/files-data - returns all FileData objects from the database as a JSON
object (not a list)
• GET /api/files-data/{id} - returns a FileData object with a specific identifier (or throws
an exception SdaException)
• POST /api/files-data - creates a FileData object and writes it to the database. Returns
the status 201 and in the Location header, the URI to get it.
• PUT /api/files-data/{id} - updates an existing FileData object stored in the database.
Returns status * 204 * (or throws SdaException when no object with the given id
exists).
• DELETE /api/files-data/{id} - removes an existing FileData object stored in the
database. Returns status * 204 * (or throws SdaException when no object with the
given id exists).
The controller should not use the repository directly. Create a simple service layer that
connects the database and web layers.
Use the following definition of the SdaException class:

Exercise 9 - solution
Part 1
application.properties file:

11
Part 2

12
Exercise 10
Create a project (https://start.spring.io/) based on Spring Boot using:
• spring-boot-starter-data-jpa
• lombok
• h2
Create the Book entity. Rows should be kept in the books table. This entity should have an
automatically generated identifier of type Long and columns such as:
• title
• author
• ISBN
13
• pagesNum
Create a JPA repository with the query:
• returning all books with a specific title
• returning the book after ISBN
• returning the author's book on a specific ISBN
• returning the 3 thickest books by a given author (the one with the most pages should
be the first, the last one with the least)
• that returns all books whose names begin with a particular String
• returning all books with pages between the two values
These queries should be constructed using the appropriate names methods. In addition,
implement a method called findWherePagesNumIsGreaterThanX and one argument of
type Integer that returns all books that have at least as many pages as the input argument
is.
Exercise 10 - solution

Exercise 11
Create a project (https://start.spring.io/) based on Spring Boot using:
• spring-boot-starter-web
• spring-boot-starter-thymeleaf
• lombok
Create a view that will display the form under the /pc-games URI (defined in
the pcgame.html file) based on the PCGameForm object. The text Create PC
Game should be above the form. The value of this text comes from the backend and is
available under the createMessage key, and the form is available under pcGameForm.
After pressing the button on the form that performs the ** post ** operation, the URI /pc-
games displays a view (defined in the file pcgame_info.html), which displays the data from
the completed form. This data is available under the key pcGame.

14
Exercise 11 - solution
pcgame.html file:

pcgame_info.html file:

15
Exercise 12
Create a project (https://start.spring.io/) based on Spring Boot using:
• spring-boot-starter-web
• spring-boot-starter-security
• spring-boot-starter-data-jpa
• h2
• lombok
Create the application's security configuration, where:
• the GET/api/cars path requires you to have the ADMIN or CARS role
• the POST/api/cars path requires you to login
• paths starting with /api/users/ require authority ROLE_USER_ADMIN
• all other paths do not require authentication
• possible ways to log in are Basic Authentication and an automatically generated login
form
• it is possible to log out
• the h2 console is available and working properly
• CSRF is not generated on paths starting with /api/
In addition, there are 3 users whose data is stored in memory:
• user admin with the roles ADMIN and CARS
• user admin2 with authority ROLE_USER_ADMIN
• user admin3 with role CARS
Each user has the password Secret_123. Bean passwordEncoder is not directly defined,
yet users should be able to log into secured paths.
Exercise 12 - solution

16
Task 13
Create a project (https://start.spring.io/) based on Spring Boot using:
• spring-boot-starter-web.
• spring-boot-starter-data-jpa.
• h2
The goal of the task is to create a REST API for managing bookings. As part of its
implementation, it is necessary to implement 3 layers:
• database service layer
• business logic service layer
• web layer
Each of the above should be verified by unit/integration tests
Repository

17
Create an Reservation entity. The rows held should be in a reservation table. This entity
should have an automatically generated identifier of type Long and columns:
• name.
• hotelName.
• numberOfPeople.
• standard.
• price.
• startDate
• endDate.
Create a JPA repository with a query:
• returning all reservations with a specific name.
• returning all reservations with the indicated standard and price.
• all reservations active on the indicated date.
Develop tests using DataJpaTest.
Business logic handling layer
Implement the mechanism:
• adding reservations to the database
• updating reservations
• returning all reservations
• returning reservations by name
• deleting reservations
Based on the implementation, prepare unit tests
Web layer
Implement the following REST API methods:
• POST: add reservations to the database.
• PUT: updating a reservation
• GET: returning all reservations
• GET + @PathVariable: returning reservations by name.
• DELETE: deleting a reservation.
Based on your implementation, prepare unit tests.
Integration tests
In order to verify the correct operation of the application, implement integration tests to
check the correct integration of the different layers.
Task 13 - answer

18
Repository
Implementation

19
Tests

20
21
Servis
Implementation

22
Tests

23
24
Controller
Implementation

25
Tests

26
Integration tests

27
28

You might also like