[英]How to use session_id for REST calls?
I'm making a multiple service calls to a service. 我正在对一个服务进行多个服务调用。 Let's say i have to make 10 POST calls to a service.
假设我必须对服务进行10次POST调用。 Each service require session_id cookie.
每个服务都需要session_id cookie。 If it's not provided, then the service will generate one and use it for all the service calls.
如果未提供,则该服务将生成一个并将其用于所有服务调用。
In my app i make several consecutive service calls. 在我的应用程序中,我进行了多次连续的服务呼叫。 I can invoke those calls again in the "loop", but i need a different session_id for a single iteration.
我可以在“循环”中再次调用这些调用,但是单次迭代需要使用不同的session_id。
In the code MyService component is making a service call. 在代码中,MyService组件正在进行服务调用。 Filter of that service will generate session_id.
该服务的过滤器将生成session_id。 But invoke() method might be invoked multiple times and I want filter to generate a different session_id for each invocation of invoke() method.
但是invoke()方法可能会被多次调用,我希望过滤器为invoke()方法的每次调用生成一个不同的session_id。 Currently, session_id is getting generated once
目前,session_id生成一次
@Component
class MyService {
@Autowired
private RestTemplate restTemplate;
/**
* T - is a request type
* R - is a response class type
*/
private <T,R> Optional<R> doPost(String url, T request, Class<R> responseType) {
return Optional.ofNullable(restTemplate.postForObject(url, request, responseType));
}
public void invoke() {
doPost("url1", someRequest1, SomeResponse1.class);
doPost("url2", someRequest2, SomeResponse2.class);
}
}
I can create a session_id in invoke() method and make sure that a different session_id is used to make these calls. 我可以在invoke()方法中创建一个session_id,并确保使用不同的session_id进行这些调用。 All service calls in invoke() will have the same session_id and next invocation will have a different one.
invoke()中的所有服务调用将具有相同的session_id,而下一次调用将具有不同的session_id。 But i am not sure if this approach is a right way to achieve this goal.
但是我不确定这种方法是否是实现此目标的正确方法。
What can you advise me to use? 您可以建议我使用什么?
session_id is generated by each client (if you test from other web browser you will see a different session_id), the only way that receive a different for each request is invalidating the session, althoung if your require a uuid by request use a listener session_id由每个客户端生成(如果您从其他Web浏览器进行测试,您将看到一个不同的session_id),则为每个请求接收不同内容的唯一方法是使会话无效,如果您要求按请求使用uuid,则使用监听器
public class ProjectRequestListener implements ServletRequestListener {
@Override
public void requestInitialized(ServletRequestEvent requestEvent) {
requestEvent.getServletRequest().setAttribute("requestId", UUID.randomUUID());
}
@Override
public void requestDestroyed(ServletRequestEvent requestEvent) {
requestEvent.getServletRequest().setAttribute("requestId", null);
}
}
and add the listener to the project web.xml 并将侦听器添加到项目web.xml
<listener>
<listener-class>
com.project.server.ProjectRequestListener
</listener-class>
</listener>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.