I need to send a JSON file to API that receives it and processes further. The main issue I’m having is initializing that POST method at application startup. I’ve created the JSON files, so only that is left over is to activate POST method when the application starts.
I know this is for demonstrating purposes but still haven’t found anything like that in Springs documentation.
Basically when the SpringBoot application is run, the first thing that it does is call a post method and sends those JSON files to API. Any ideas?
2
Answers
You can use ApplicationReadyEvent listener. Whenever the application will start it will execute one time. Make sure the record / class should be a component.
You can do this in a couple of ways:
EventListner
Handler (this is what I prefer)Configuration
with a@PostConstruct
which has the logic – this is something I don’t prefer cause it might not always be the one to run post-startup, and in this case, we needRestTemplate
so we need to think ofOrder
of our configuration class, which I think we should tend to avoid as much as possible.For
EventListener
, you can have the below Component which executes on theApplicationStartedEvent
.More details can be found here in the official docs