为 JAX-RS 2.0 Client API 设置请求超时 [英] Setting request timeout for JAX-RS 2.0 Client API

查看:31
本文介绍了为 JAX-RS 2.0 Client API 设置请求超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了简单的 REST Web 服务客户端类,它使用 JAX-RS 2.0 客户端 API 来发出 REST 请求.我想弄清楚如何为每次调用设置请求超时.这是请求的代码:

I have written simple REST web service client class which uses the JAX-RS 2.0 client API to make REST requests. I am trying to figure out how to set a request timeout for each invocation. Here is the code for a request:

Client client = ClientBuilder.newBuilder().build();
WebTarget resourceTarget = client.target(restServiceUrl)
        .path("{regsysID}/{appointmentID}/")
        .resolveTemplate("regsysID", regSysId)
        .resolveTemplate("appointmentID", apptId);

Invocation invocation = resourceTarget.request(MediaType.APPLICATION_JSON).buildPut(null);
String createSessionJson = invocation.invoke(String.class);

推荐答案

您可以先创建一个 ClientConfig,然后在创建新客户端时将其作为参数提供.

You can do this by creating a ClientConfig first and providing it as an argument when creating the new client.

import org.glassfish.jersey.client.ClientProperties;

ClientConfig configuration = new ClientConfig();
configuration.property(ClientProperties.CONNECT_TIMEOUT, 1000);
configuration.property(ClientProperties.READ_TIMEOUT, 1000);
Client client = ClientBuilder.newClient(configuration);

这篇关于为 JAX-RS 2.0 Client API 设置请求超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆