I cannot find anything about API versioning in the oficial site of Spring Rest Data
I found a Joshua Long presentations about REST APIs with Spring
API versions can be implemented with one of two ways:
The first approach have some problems described here
Through a URI path – you include the version number in the URL path of the endpoint, for example, /api/v1/persons. Through query parameters – you pass the version number as a query parameter with a specified name, for example, /api/persons? version=1.
Advantages of using Spring Boot A few benefits of using Spring Boot for your REST APIs include: No requirement for complex XML configurations. Embedded Tomcat server to run Spring Boot applications. An auto-configuration feature by Spring Boot that configures your application automatically for certain dependencies.
This is an interesting topic. The documentation of spring-data-rest does not mention best-practices for versioning.
From what I know about spring-data-rest it is not possible to implement media type versioning without implementing custom controllers.
But of course the current functionality allows for URI based versioning:
com.company.v1.MyEntity
or com.company.MyEntityV1
in the repository use @RepositoryRestResource
to contain the version in the URI
@RepositoryRestResource(path = "/v1/shops")
So you could always create a new entity version when you have breaking changes on the API and use a new repository to expose the new version on the API.
EDIT: We were discussing this topic in the team and a few new interesting thoughts on REST API versioning came up that I would like to share.
I think we all agree that the idea outlined above is not an elegant one. It introduces a lot of code duplication and when you switch off the old version you have to remove it from the code base - which can be harder than you think it is. In the meantime you have to maintain two versions in the code base. You would always want to avoid that.
So what are the alternatives. You could do what we often do in a microservices world - push complexity to the infrastructure. Creating and running new runtimes is fairly easy in a microservices infrastructure so we could choose to run two different versions of the same service. The old version providing the old API version and the new one supporting our new version. An API Gateway could take over the routing e.g. based on HTTP headers (Accept or Content-Type) or version information contained in the URI. And if you want to get rid of the old version you just have to switch off the runtimes that run the old version and you are done. I think this can be an elegant solution that keeps your code base clean.
There are chances that your infrastructure already supports running different versions of the same service to support blue-green deployment scenarios. Which means you already have some routing capabilities that you could build up on.
Also I think if you support blue-green deployment scenarios (which you should) you have to keep database schema versions compatible between releases (e.g. your old version of your service needs to be able to run on the new schema version). If you have to keep this level of compatibility it might not be too hard to avoid REST API versioning most of the times. So you are not forced to do it often - maybe only if you make fundamental changes (that you would not do in the same codebase anyway).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With