Spring Boot Starter provides an Offline Mode for Spring Boot application.
- Add dependency to your project
Maven
<dependency>
<groupId>pl.maciejkopec</groupId>
<artifactId>offline-mode-spring-boot-starter</artifactId>
<version>2.0.11</version>
</dependency>
Gradle
implementation 'pl.maciejkopec:offline-mode-spring-boot-starter:2.0.11'
- Enable Offline Mode in
LEARNING
mode
application.yaml
offline-mode:
mode: learning
enabled: true
- Mark method with
@OfflineMode
annotation
@Service
class DemoService {
@OfflineMode
public String demo() {
return "test" + LocalDate.now();
}
}
- Run your application. This library will capture the returned data and store serialized data in
offline
folder. - Turn Offline Mode in
SERVING
mode
application.yaml
offline-mode:
mode: serving
enabled: true
The execution of demo()
method won't happen now. Instead, the return data will be served from serialized file captured
in the previous step.
See documentation for more examples.
- Spring Boot 3
- Java 17
- Spring Boot 2
- Java 16
This starter is useful when you want to avoid calling external services, databases etc. There are a few possible use-cases, e.g.
- when you cannot run all services your application depends on in the local environment, and you want to run your application without calling these services,
- when you want to avoid calling services that are billed per call,
- when your development environment is not stable and impacts the development of the service that you own,
- when data quality is poor or hard to replicate,
- when you are performing PoC,
- when the service you want to use is not ready yet, it is easy to mock the response of such service,
- and more!
For the cache to work you first need to make a call. If the returned data depends on the input parameters, that becomes a problem, because you need to make many calls to capture data. Also, the cache's main purpose is to avoid expensive calls, once data is cached it doesn't change.
With this starter, you can prepare data on your own. You can run your application in LEARNING
mode to speed this
process up, but nothing stops you from creating the response file from scratch. It is very easy to provide many
responses based on the input parameters, which makes this tool great for testing and development!
Maps with complex keys are not supported.
This is not meant for production! The problems this starter helps to solve are mainly related to lower environments or PoC-type projects. If you cannot depend on some service in production then you should look for a different solution.
It should not be a replacement for caching solution.
Feel free to use any part from this repository.