I am developing a REST API which accepts JSON using Spring Boot. I use Spring Security for authentication. I have a use case where I have two services, one to test connection to a 3rd party system and other one to fetch data from the 3rd party system.

  • api/system/connection
  • api/system/customData

When I call the first service, I check if I am able to connect successfully to the 3rd party system. If yes I return a "Success" or a "Failure" message. When I try to make the second call to get customData, I expect the request to have the login information to the 3rd party system to be present in the request. I again create a new connection and fetch the custom data and return the data. The problem with this approach is that I create connection objects every time which consumes a lot of time for each request.

In order to avoid this overhead I should be changing the second request as this


But for doing this, what should I return in the first service that tests the connection?

  1. Should I return the connection itself? Is it possible to convert the connection object to a JSON and deserialize it when it comes in the second request? Is it also a secure thing to do?
  2. Should I cache this connection to 3rd party system at the server side and return a unique id for each connection? But does this not break the RESTful nature of the API?

Or is there a different approach to how I can tackle this problem.


  • 1
    why do you need to do the check? just get rid of the first method
    – Ewan
    Commented Feb 4, 2018 at 7:41
  • Should I cache this connection to 3rd party system No, unless you consider that establishing the connection is costly in terms of resources and time. Otherwise, my humble advice is open and close the connection as soon as possible. Think in terms of concurrency and load-balancing.
    – Laiv
    Commented Feb 4, 2018 at 12:43
  • Get rid of the first connection. Totally. Because it's just causing latency and introducing complexity. Do request the 2nd endpoint directly, handle the server-to-server communication errors and respond accordingly. If the server-to-server communication is costly. Could you cache 3rd party response to customData?
    – Laiv
    Commented Feb 4, 2018 at 15:21

2 Answers 2


In this particular case I do not think you need the first request at all as @Ewan noticied.

But if you do see such a need, you can pass a state using some session token (much like OAuth Bearer authentication token. Using this token you can create and maintain a session at your webservice, which can hold some state information between requests.

But please do think twice. REST API is better without any state management.


Ok, from my experience. You use ApacheHttpClient to execute request to APIs. Health check of API makes sense if you want to know early about it. For example, you perform some batch operations - making hundreds or thousands of calls to API. But mostly there are HTTP response codes from API, connection timeout, exception to handle the problematic case. So nobody usually does two calls to verify that connection is fine and then make a call you need. Regarding creating a connection - check ApacheHttpClient 4.0 library. There's connection manager there. If you need lots of calls to be made then threading might be useful. If it takes 250-400 ms to get a response, you might have 50 parallel calls to get 50 responses for basically same time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.