April 26, 2025
Spring rest framework - SRF

Spring Rest Framework (SRF) A Project Inspired by Django Rest Framework

For years, I’ve been deeply immersed in the Django ecosystem, developing APIs and building applications using Django and Django Rest Framework. DRF has always worked as magician for me, significantly reducing boilerplate code while offering robust flexibility. On the flip side, I’ve also spent a fair share of time working with Spring and Spring Boot. While Spring is fantastic in many ways, I noticed the gap when it came to a DRF-like experience. The absence of a framework that could remove boilerplate codes, streamline API development in the Spring world, and provide a similar level of flexibility was something that always bugged me. So, I decided put some effort to tackle this gap. In my spare time over the past 7 months, I’ve been working on creating a framework like DRF, designed and tailored for the Spring ecosystem. And that’s how Spring Rest Framework was born.

The goal was to build a framework that would reduce boilerplate code, simplify the filtering, serialization, and validation in the regular web APIs development to expose CRUD operation for models, and bring some of the fantastic features of DRF to the Spring world. There’s still a lot of work to be done. The framework is far from perfect, and there are numerous features and improvements that need to be added. I’d love to invite anyone who’s interested to contribute to this project. Whether you’re a seasoned developer or someone eager to get their hands dirty with open-source, your help would be greatly appreciated. There’s plenty of room for enhancement, and I welcome any bug reports, feature requests, or contributions you might have.

I’ll write about the technical aspects of Spring Rest Framework in another blog post. But to introduce the simplicity of working with this tool, here is the snippet code where you can expose a listing endpoint for city model with variety of filtering parameter:

@RequestMapping("/cities")
@RestController
@Tag(name = "City")
public class CityController extends ListController<City, Long, CityRepository> {
      public CityController(CityRepository repository) {
            super(repository);
      }

      @Override
      protected Class<?> getDTO() {
            return CityDto.class;
      }
      
      @Override
      protected FilterSet configFilterSet() {
      return FilterSet.builder()
            .addFilter("name", FilterOperation.CONTAINS, FieldType.STRING, "Check containing a name")
            .addFilter("continent", "country__continent__name", FilterOperation.CONTAINS, FieldType.STRING, "Check containing a continent name")
            .addFilter("number_of_people", "population", FilterOperation.BETWEEN, FieldType.INTEGER, "Retrieves cities with population between provided population range")
            .build();
      }
}  

In the example the continent (mapped to country__continent__name) Checks for a substring in related model continent‘s name. name filter parameter checks if the name contains a given string. And number_of_people (mapped to population field of City model) filters cities within a specific population range in specific continent.

In this example the presence of filter parameters are optional, so the endpoint can be called like this to query the database records:

curl -X GET "http://localhost:8080/cities?continent=Europe&number_of_people_from=140000&number_of_people_to=250000"

If you liked it, please give the project a star on Spring Rest Framework Github repository. Here are some helpful resources to get you started:

  1. Official Spring Rest Framework Documentation
  2. Learn Spring REST Framework (SRF) With Example
  3. Advanced Spring REST Framework (SRF) Tutorial with Example.
  4. Validation in Spring REST Framework

Happy coding! 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *