Slides and resources
Slides
Resources
Labs starting point – Spring Initializr: start.spring.io
Spring – spring.io
Baeldung – lot of useful articles about Spring and Java: baeldung.com
Excercises
Set I – basic web
Task 1 (1 point)
- Go to start.spring.io
- Add following dependencies:
a) Spring Web
b) Spring Data JPA
c) h2 database - Generate project
- Import to IDE
- Run ./gradlew :bootRun (after a short moment You should see Started DemoApplication in …, it’s alright that gradle never reaches 100%)
Task 2 (1 point)
- Create HelloController class with GET endpoint /hello that will respond with “Hello World!”
- Go to browser or curl localhost:8080/hello (you should see Hello World! as output)
Task 3 (1 point)
- Add parameter to /hello endpoint (use PathVariable) that will ask for name.
a) respond with “Hello <provided name>!”
Task 4 (2 points)
- Add application property app.demo.secret_name with your own value
UPDATE on 21.05.2024 19:07 Please just use if else for task below instead of switch - In GET endpoint use switch-case construction (the one with arrows) to distinguish 3 possibilities:
a) name equal to secret_name value -> respond with “Nice to see You!”
b) name is null -> respond with “Hello World!”
c) otherwise -> “Hello <provided name>!”
Set II – CRUD
Create a fundamentals for shop application
Task 1 (1 point)
Create Product class with @Entity annotation – with fields: id, name, description
Task 2 (1 point)
Create crud repository for Product
Task 3 (3 points)
- Add following endpoints to new controller (use repository inside controller):
a) POST to create single product
b) GET to get all products
c) GET with parameter to get single product (filter by id)
Set III – app enhancement
Task 1 (5 points)
- Create Order class – with fields: id, list of products (find the right annotation to bind entities), customer name, customer email, and order_status (public enum inside this class with at least 3 values)
- Create crud repository for Order
- Create endpoints for Order operations:
a) POST to create new order (with one or many products)
b) PUT to update order status - Run application and play with endpoints
a) verify if they work as they should
b) what’s missing if this would be production ready application? - Add validation of shop requests (use ideas from previous point and use at least 3 validations)
Endpoints should respond with HTTP status 400 and give meaninful information to api user
*hint – use @Valid annotation
Bonus task
Add basic security mechanism (basic auth) to the application (for simplicity – use in-memory user database with hardcoded usernames and passwords)
a) there should be 2 users: admin (with ADMIN_ROLE) and customer (with CUSTOMER_ROLE)
b) only admin can create products
c) only admin can update order status
d) only customer can create order
e) both admin and customer can see all products