Skip to content

Marcin Baranowski

Java dev

  • Spring workshops
  • Presentations
  • Contact

Spring workshops

Slides and resources

Slides

final-Java-and-SpringDownload


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)

  1. Go to start.spring.io
  2. Add following dependencies:
    a) Spring Web
    b) Spring Data JPA
    c) h2 database
  3. Generate project
  4. Import to IDE
  5. 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)

  1. Create HelloController class with GET endpoint /hello that will respond with “Hello World!”
  2. Go to browser or curl localhost:8080/hello (you should see Hello World! as output)

Task 3 (1 point)

  1. Add parameter to /hello endpoint (use PathVariable) that will ask for name.
    a) respond with “Hello <provided name>!”

Task 4 (2 points)

  1. 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
  2. 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)

  1. 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)

  1. 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)
  2. Create crud repository for Order
  3. Create endpoints for Order operations:
    a) POST to create new order (with one or many products)
    b) PUT to update order status
  4. 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?
  5. 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

Marcin Baranowski
| Theme: CWW Portfolio by Code Work Web.