Spring Boot vs. Quarkus: A TLDR Comparison

Spring Boot and Quarkus are two popular Java frameworks used for building modern, cloud-native applications. While both have their strengths and weaknesses, the choice between them can significantly impact your development process. Let’s dive into a quick comparison to help you decide which framework suits your needs best.

Spring Boot

Pros:

  1. Mature Ecosystem: Spring Boot has been around for a while and boasts a robust, mature ecosystem with a wide range of integrations and community support.
  2. Comprehensive Documentation: Extensive documentation and tutorials make it easier to get started and troubleshoot issues.
  3. Flexibility: It provides flexibility to configure and customize components as needed.
  4. Wide Adoption: Used by many companies, ensuring long-term viability and support.

Cons:

  1. Startup Time: Typically slower startup time compared to newer frameworks like Quarkus.
  2. Memory Consumption: Higher memory usage, which can be a drawback in resource-constrained environments.

Quarkus

Pros:

  1. Fast Startup Time: Designed to optimize startup times and memory usage, making it ideal for microservices and serverless environments.
  2. Native Image Support: Integrates well with GraalVM for creating native executables, reducing the overhead of JVM.
  3. Developer Experience: Offers features like live coding and a more streamlined development process.
  4. Kubernetes Native: Tailored for Kubernetes, making it easier to deploy and manage in cloud-native environments.

Cons:

  1. Less Mature: As a newer framework, it has a smaller ecosystem and fewer integrations compared to Spring Boot.
  2. Learning Curve: Developers familiar with Spring might find the transition challenging due to different paradigms and tools.

Use Cases

  • Spring Boot: Best suited for large, monolithic applications, enterprise-level projects, and scenarios where a mature ecosystem is essential.
  • Quarkus: Ideal for microservices, serverless applications, and cloud-native environments where startup time and resource efficiency are critical.

Example Case Studies

  • Spring Boot: Many enterprises and legacy systems use Spring Boot for its reliability and comprehensive feature set. Mad Devs outlines various scenarios where Spring Boot excels.
  • Quarkus: Organizations focused on modern, lightweight applications have adopted Quarkus for its performance benefits. For example, Quarkus is used by companies like Lockheed Martin and Cisco Systems, as detailed on Enlyft.

Conclusion

Both Spring Boot and Quarkus offer compelling features for Java developers. Your choice should depend on your project requirements, the importance of startup time and memory usage, and the need for a mature ecosystem. For traditional enterprise applications, Spring Boot remains a strong candidate. However, if you’re venturing into cloud-native development, Quarkus offers a modern, efficient alternative.

This quick comparison should help you make an informed decision tailored to your specific development needs. Happy coding!

Spring Boot Filters

Spring boot filter

What are Spring Boot filters?

Spring boot uses filters to distill HTTP requests, the process here is:

  • Intercepts the request
  • I.e The client side invoking https requests and responses

What can filters be used for?

A filter is able to perform two operations, this is done on the response and the request. This can be used to restrict URL access to users and other functionality on the request and the response.

When does a filter get invoked?

The filter comes before the controller, this determines if the request can interact with the desired controller and the response it should return.

Lets take a look at the syntax

Firstly, we start with a class that implements the interface Filter with its methods:

@Component
@Order(1)
public class ExampleFilter implements Filter {
  @Override
  public void doFilter(
    ServletRequest request,
    ServletResponse response,
    FilterChain chain
  )
    throws IOException, ServletException {
    HttpServletRequest servletRequest = (HttpServletRequest) request;
    chain.doFilter(request, response);
  }
}

As shown above the syntax has some fundamental syntactical areas that provide configuration to filter on how we apply them, lets go through this:

  1. Filters are uses to incept or filter http response and request instance
  2. Operations can be performed on the request instance (before sending the response instance to the controller)
  3. Operations can be performance on the response instance (before sending the response back to the requested client)
  4. @Component annotation is used in a spring boot application in order to utilise filter class
  5. javax.servlet.Filter is the package used for this
  6. The @Order annotation allows us to order our filter as we can use multiple filter!

Lets take a deeper dive on how this works…

  1. @Component annotation is used alongside Filter implementation in order to initialise the class on startup in spring
  2. @Order(1) annotation is used to execute the filter first in the application
  3. After this, we need a class that implements the Filter interface from Servlet, this interface bring in doFilter() method
  4. So for the example above we have overridden the filter methods inside the class
  5. The doFilter() method will trigger if we receive any request or any HTTP request from the client (This is why we have ServletRequest and ServletResponse objects)
  6. At this point the ServletRequest object is then used casted to the HttpServletRequest object, and this attempt to get the URI from it
  7. This doFilter() method will now hand over the object to the controller, and all the required operations with get performed there 🤗
  8. Once all the operations are done! The control returns back to this doFilter() method again
  9. Loggers could be place here to monitor requests and response details (useful for debugging)

Final Note

Adopting filters are incredibly powerful for when we want to do things before and after receiving or responding to requests! Additionally, we can restrict URL, and there are also some more out the box implementations of filters that adopt security, such as BasicAuthenticationFilter (that can be used to support (OAuth2 implementation) or you can create your own custom filters.