Back-end REST API for the Spawn Mobile App, written in Java Spring Boot, and connected to a MySQL database. Plus, Redis caching, JWTs for authorization, and OAuth Google & Apple sign-in.
Table of contents:
Spring Boot is our back-end framework with the Java language. It handles the API requests and responses, between our controller, service, and repository layers (see above in here).
- Spring Annotations
@RestControllertells Spring that this class is a controller, and that it should handle incoming HTTP requests (GET, POST, PUT, DELETE, etc.)- We have various mappings for these types of requests, like
@GetMapping,@PostMapping,@PutMapping,@DeleteMapping, etc.- This is also where we specify the path of these requests, along with:
@PathVariablefor URL parameters@RequestParamfor query parameters@RequestBodyfor the request body (in a POST request, for example)
- This is also where we specify the path of these requests, along with:
- We have various mappings for these types of requests, like
@Servicetells Spring that this class is a service, and that it should be managed by Spring@Autowiredtells Spring to inject the dependency (e.g.ActivitieserviceintoActivityController)@Repositorytells Spring that this class is a repository
- Beans
- Beans refer to the instantiations of our classes, which get managed by Spring. For example, our service classes, since they are concrete implementations of our interfaces (e.g.
UserService, being the implementation ofIUserService), are beans. - So, we don't have to manage circular dependencies between classes. For example, since
UserServicetakes inActivitieserviceas a dependency, and vice versa,Activitieservicetakes inUserService, we can annotate them with@Autowiredto let Spring handle that issue
- Beans refer to the instantiations of our classes, which get managed by Spring. For example, our service classes, since they are concrete implementations of our interfaces (e.g.
@Entitytells JPA that this class is an entity, and that it should be mapped to a table in the database@Idtells JPA that this field is the primary key of the table@GeneratedValuetells JPA that this field is auto-generated- There are also other strategies for generating primary keys, like
GenerationType.IDENTITY,GenerationType.SEQUENCE, etc. - For ids of a table, there are also
@EmbeddedIdfor composite primary keys, for example in theActivityParticipantstable
@Columnis used to specify the column name, length, nullable, etc.@OneToManyand@ManyToOneare used to specify the relationships between entitiesJpaRepositoryis an interface that extendsCrudRepository, which provides CRUD operations for the entity- So, there are pre-defined generic methods like
save,findById,findAll,delete, etc. - We can also define custom queries in the repository interface, by using the
@Queryannotation- An example of a custom query is in the
UserRepositoryinterface, where we find users by their username - Also, with custom queries, they can be generated for us by simply naming the method according to JPA standards, like in the
ActivityRepositoryinterface, where we find Activities by their start time using the method,List<Activity> findByCreatorId(UUID creatorId);
- An example of a custom query is in the
- So, there are pre-defined generic methods like


