Git Product home page Git Product logo

realworld-java21-springboot3's Introduction

RealWorld Example App

Java 21 + Spring Boot 3 codebase containing real world examples (CRUD, auth, advanced patterns, etc) that adheres to the RealWorld spec and API.

This codebase was created to demonstrate a fully fledged fullstack application built with Java 21 + Spring Boot 3 including CRUD operations, authentication, routing, pagination, and more.

I have gone to great lengths to adhere to the Java 21 + Spring Boot 3 community styleguide & best practices.

For more information on how to this works with other frontends/backends, head over to the RealWorld repo.


Table of Contents


Considerations

While implementing this application, I had the following considerations. If there are any developers who are trying to learn by looking at this repository, or who are planning to develop something based on this repository, it might be helpful to consider these points:

  1. Ensure that the application handles errors gracefully and logs them appropriately. This will make it easier to debug issues in production and improve the overall reliability of the application.
  2. Scalability: Consider the scalability of the application, especially if it is expected to handle a large volume of traffic or data. For example, a User class can have both followers and following, but will this code work properly if the number of followers is in the hundreds of millions?
  3. Make sure the application is secure, including protecting against common attacks such as SQL injection, cross-site scripting (XSS), and cross-site request forgery (CSRF). Use proper authentication and authorization mechanisms to ensure that only authorized users can access sensitive data or perform critical actions.
  4. Write clean, maintainable code that is easy to understand and modify. Use best practices such as code commenting, code reviews, and version control to make it easier to maintain and evolve the application over time.
  5. Performance: Optimize the performance of the application, especially for frequently used or resource-intensive operations. This includes things like database indexing, query optimization, and caching.
  6. If you implement JWT yourself, even if you use Spring Security, you will have to write a lot of code. That's why I used OAuth 2.0 Resource Server, which makes implementation much simpler.
  7. I have encountered various frameworks such as Django, Flask, FastAPI, Express, NestJS, Ktor, Quarkus, but I have not seen any framework as great as Spring yet. (The backward compatibility guaranteed by the Java ecosystem is truly amazing.) Therefore, I implemented it in a tightly coupled manner with the Spring framework, assuming that the framework would not change in the future.
  8. I was implemented it so that the dependency on the database layer is only through JPA. This makes it very easy to change databases. However, if you have a deep understanding of the database, you may be concerned about inefficient queries.
  9. I used H2 for the database. Although H2 is a memory database, it operates in MySQL mode and provides almost the same functionality as MySQL.
  10. I have excluded input validation. If necessary, it can be easily implemented using JSR-303(380) or similar technologies, so there should be no problem.
  11. I struggled with whether the slug and title of the article table should be unique, but ultimately decided to make them unique in accordance with the API specification.
  12. Dependencies were configured to converge to the domain package as much as possible. Dependencies can be more easily identified by looking at the import statement of each class.

How it works

The RealWorld project aims to create mini-blog applications with the same specifications using various technology stacks, allowing for a comparison between them.

This application provides the following key features:

  1. Authenticate users via JWT (login/signup pages + logout button on settings page)
  2. CRU- users (sign up & settings page - no deleting required)
  3. CRUD Articles (offset based pagination, filtering by facets, sorting)
  4. CR-D Comments on articles (no updating required)
  5. Favorite articles
  6. Follow other users

Project structures

The project is implemented based on Java 21 and Spring Boot 3, utilizing various Spring technologies such as Spring MVC, Spring Data JPA, and Spring Security. It uses H2 DB (in-memory, MySQL mode) as the database. and JUnit5 for writing test codes.

To run the project, JDK(or JRE) 21 must be installed first. Then, execute the ./gradlew bootRun command in the project root directory to run the application. After that, you can use the application by accessing http://localhost:8080 in your browser.

Taking a closer look at the project structure, the main code of the application is located in the src/main/java directory, while the test code is located in the src/test/java directory. Additionally, configuration files and such can be found in the src/main/resources directory.

The application's package dependencies and core logic are implemented as follows:

Modules

image

core, persistence, api and modular-application modules exist, and each module performs the following roles.

  • core: Contains the core logic of the application, including the domain model, service, and exception handling.
  • persistence: Contains the persistence layer logic of the application, including the repository and entity.
  • api: Contains the API layer logic of the application, including the controller and DTO.
  • modular-application: All existing modules are put together to form an executable application.

Classes

  • ~Controller: Processes HTTP requests, calls business logic, and generates responses.
  • ~Service: Implements business logic and interacts with the database through Repositories.
  • ~Repository: An interface for interacting with the database, implemented using Spring Data JPA.

By using ~Repository in the core package as an interface, I was applied DIP so that the dependency of the persistence package is also directed to the core package. Finally, I was configured the dependencies so that all packages in the project depend on the core package. Please refer to the import statements for each class.

Authentication and authorization management are implemented using Spring Security, with token-based authentication using JWT. Moreover, various features of Spring Boot are utilized to implement exception handling, logging, testing, and more.

Through this project, you can learn how to implement backend applications based on Spring and how to utilize various Spring technologies. Additionally, by implementing an application following the RealWorld specifications, it provides a basis for deciding which technology stack to choose through comparisons with various other technology stacks.


Database architecture

Note: I paid attention to data types, but did not pay much attention to size.

Many developers who use JPA tend to use Long as the id type. However, it's worth considering whether your table with an id of Long will ever need to store 2^63 records.

image


Getting started

Note: You just need to have JDK 21 installed.

Note: If permission denied occurs when running the gradle task, enter chmod +x gradlew to grant the permission.

Run application

./gradlew :modular-application:bootRun

Check code style

Note: When you run the build task, this task runs automatically. If the code style doesn't match, the build will fail.

./gradlew spotlessCheck

Apply code style

./gradlew spotlessApply

Run build

./gradlew build

Run test

Note: Running this task will generate a test coverage report at build/jacoco/html/index.html.

./gradlew test

Run E2E test

  1. Run application (important)
  2. Run E2E test

Performance

image

realworld-java21-springboot3's People

Contributors

shirohoo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

realworld-java21-springboot3's Issues

./gradlew bootRun does not work. Build failure

Hi

I was trying to follow the readme, but unfortunately, I cannot run the app.

Executing the ./gradlew bootRun results in the error:

Execution failed for task ':bootRun'.
> Failed to calculate the value of task ':bootRun' property 'mainClass'.
> Main class name has not been configured and it could not be resolved from classpath

System:
MaOS Sonoma 14.2
Java 21

image

Build failure

Hi

I was trying to run the app according to the readme but i've encountered an error.

Executing the ./gradlew bootRun results in the error:
image

This method cannot decide whether these patterns are Spring MVC patterns or not. If this endpoint is a Spring MVC endpoint, please use requestMatchers(MvcRequestMatcher); otherwise, please use requestMatchers(AntPathRequestMatcher).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.