简体   繁体   中英

Use response from a restful webservice endpoint call to be used later on in some other webservice endPoint call

I want response from one webservice call to be used later on by some other webservice call. How do I implement the code flow for it. Do I have to maintain a session? I am creating the restful webservices using Spring,java If user 1 calls an endPoint /getUserRecord and 1 minute later calls /checkUserRecord which uses data from the first call, how to handle it since user 2 can call /getUserRecord before user 1 calls /checkUserRecord I am using Spring java to create RESTFul webservices.

Thanks, Arpit

If you are using Spring Boot, it provides a way to enable caching on your Repository object which is what you are trying to solve.

@EnableCaching
org.springframework.cache.annotation.EnableCaching

You can use UserID as a hash attribute while creating your key so that response from different users remains unique.

technically you can pass UserRecord from get to check.

GET /userrecords/ --> return the one or more record

POST /checkUserRecord with the record as you want to check as request body.

But I strongly advise you to not do this . Data provided by client are unreliable and cannot be trust by your backend code. What if some javascript has altered the original data ? Besides, what if you have a list of data or heterogenous payload to pass back and forth, it would end up to a complete mess of payload exchanges from client and server.

So as Veselin Davidov said, you should probably stick with clean stateless REST paradigm and rely on identifier:

so

GET /userrecords/ --> [ { "id": 1, "data":"myrecorddata"}, ...]

GET /checkUserRecord/{id} like /checkUserRecord/1

and yes, you will have to make two calls to the database. If your concern is performance, you can set some caching mecanism as piy26 points out, but caching could lead you to other issues (how to define a proper and reliable caching strategy ?).

Unless you manage a tremendous amount of data, I think you should first focus on providing a clear, maintainable and safely usable REST API with stateless design.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM