简体   繁体   English

使用来自宁静的Web服务端点调用的响应,以便稍后在其他一些Web服务EndPoint调用中使用

[英]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. 我希望一个Web服务调用的响应稍后被其他Web服务调用使用。 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. 我正在使用Spring,java创建宁静的Web服务,如果用户1调用了一个EndPoint / getUserRecord,并且一分钟后调用了/ checkUserRecord,它使用了第一次调用中的数据,那么由于用户2可以在用户1调用/ checkUserRecord之前先调用/ getUserRecord,因此如何处理它我正在使用Spring Java创建RESTFul Web服务。

Thanks, Arpit 谢谢,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. 如果您使用的是Spring Boot,它提供了一种在您的存储库对象上启用缓存的方法,而这正是您要解决的问题。

@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. 您可以在创建密钥时将UserID用作哈希属性,以使来自不同用户的响应保持唯一。

technically you can pass UserRecord from get to check. 从技术上讲,您可以从get传递UserRecord进行检查。

GET /userrecords/ --> return the one or more record GET / userrecords /->返回一个或多个记录

POST /checkUserRecord with the record as you want to check as request body. POST / checkUserRecord与要作为请求正文进行检查的记录。

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 ? 如果某些javascript更改了原始数据怎么办? 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: 因此,正如Veselin Davidov所说的那样,您应该坚持使用干净的无状态REST范例并依靠标识符:

so 所以

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

GET /checkUserRecord/{id} like /checkUserRecord/1 GET / checkUserRecord / {id}像/ 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 ?). 如果您关心性能,可以按照piy26的说明设置一些缓存机制,但是缓存可能会导致您遇到其他问题(如何定义适当且可靠的缓存策略?)。

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. 除非您管理大量数据,否则我认为您首先应该专注于提供具有无状态设计的清晰,可维护且安全可用的REST API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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