Skip to content

Make School Tutorial - Make Parties - Reworked for Spring Boot

Notifications You must be signed in to change notification settings

jdegand/make-parties-spring-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Make Parties Spring Backend

This is a conversion of a past MakeSchool tutorial I completed to Spring Boot.

Screenshots








Built with

  • Java 17
  • Spring Boot 3.1
  • H2 Console
  • Lombok

Thoughts

  • Doing the mappings between entities is easy but there can be a few gotchas.
  • With Event and Rsvp, there exists a cyclic dependency and you get a stack overflow if you try to get Event - either use an annotation or a dto. See Stack Overflow for more.
  • Having to use DTOs with mappings (ManyToOne etc) is glossed over and obscured in a lot of implementations.
  • Using DTOs requires extra work -> easier to create a bug.
  • @JsonBackReference can't be used on a collection.
  • Could add a third table for the list of rsvps -> @JoinTable
  • Using Builder on an entity with a mapping warns - "@Builder will ignore the initializing expression entirely. If you want the initializing expression to serve as default, add @Builder.Default. If it is not supposed to be settable during building, make the field final." -> Because I set rsvps to an empty list, I needed to use @Builder.Default annotation on rsvps.
  • Added takesPlaceOn to Event Entity (stretch challenge in original MakeSchool tutorial).
  • Added timestamps as well but I did not use them in the frontend.
  • Added validation after I had the frontend working with the backend.
  • The tutorial did not feature any validation.
  • I kept the validation fairly minimal - a few required properties and date validation for takesPlaceOn.
  • In the entities, I used {entity}Id instead of just id. This can be annoying and debatable if this is a good practice or not.
  • Tests were written in this order: repository layer, service layer, controller layer.
  • I am not sure if it is truly necessary to test methods that just use JPARepository methods - seems redundant - this project didn't use any custom queries.
  • However, the repository tests are easy to write.
  • Had trouble with using Optional and thenReturn together in the EventService Tests - the commented out code seems like it should work but maybe I missed something - could be the return type of the function - ie Optional vs Event
  • You need to use assertAll() to handle void methods ie delete.
  • Not using response entities does create problems when testing error states in the controller layer because requests will fail without giving the appropriate status code. You have to write more checks to test routes versus stopping after isBadRequest() etc.

Continued Development

  • Use ResponseEntity for all routes?
  • Better exception handling / Custom exceptions
  • The validation for event put requests needs improvement - need to check takesPlaceOn is a valid date
  • fetchEventById in the event service implementation could probably be refactored and done in a better way

Useful Resources

  • YouTube - OneToMany | ManyToOne Relationship In Hibernate | SpringBoot | JPA | Java
  • Stack Overflow - code architecture of service interface and service implementation
  • Stack Overflow - timestamps
  • YouTube - Build REST API with Spring Boot and JPA [2021] - 25 Create timestamps by JPA
  • YouTube - Spring Boot For Beginners - One-To-Many in Spring Data
  • Baeldung - circular dependencies
  • Stack Overflow - fasterxml jackson stack overflow
  • Github - pokemon review springboot
  • Stack Overflow - one to many column not showing
  • Stack Overflow - stack overflow for hibernate one to one mapping
  • Stack Overflow - bidirectional relationships and infinite recursion
  • YouTube - 40 Enable CORS in Spring Boot
  • Stack Overflow - cors is not rejecting messages when i expect
  • Stack Overflow - return json for response entity
  • YouTube - Spring Boot tutorials | Spring boot full course - How to use ResponseEntity in REST API?
  • Stack Overflow - implement status 200 when delete is sent
  • Stack Overflow - jpa jointable annotation
  • Baeldung - java string valid date
  • How to Do in Java - date validation
  • Baeldung - lombok builder default value
  • Stack Overflow - suppress lombok warnings
  • YouTube - Spring Boot Unit Testing Tutorial (W/ Mockito)
  • Stack Overflow - mockito error with method that returns optional
  • Baeldung - java init list one line
  • YouTube - Write Unit Test Case For Controller Layer using Junit & Mockito | @WebMvcTest |MockMvc

About

Make School Tutorial - Make Parties - Reworked for Spring Boot

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages