本文整理汇总了Java中org.springframework.web.client.RestOperations类的典型用法代码示例。如果您正苦于以下问题:Java RestOperations类的具体用法?Java RestOperations怎么用?Java RestOperations使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
RestOperations类属于org.springframework.web.client包,在下文中一共展示了RestOperations类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createCommonsHttpRestTemplate
import org.springframework.web.client.RestOperations; //导入依赖的package包/类
public static RestOperations createCommonsHttpRestTemplate(int maxConnPerRoute, int maxConnTotal,
int connectTimeout, int soTimeout, int retryTimes, RetryPolicyFactory retryPolicyFactory) {
HttpClient httpClient = HttpClientBuilder.create()
.setMaxConnPerRoute(maxConnPerRoute)
.setMaxConnTotal(maxConnTotal)
.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(soTimeout).build())
.setDefaultRequestConfig(RequestConfig.custom().setConnectTimeout(connectTimeout).build())
.build();
ClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
RestTemplate restTemplate = new RestTemplate(factory);
//set jackson mapper
for (HttpMessageConverter<?> hmc : restTemplate.getMessageConverters()) {
if (hmc instanceof MappingJackson2HttpMessageConverter) {
ObjectMapper objectMapper = createObjectMapper();
MappingJackson2HttpMessageConverter mj2hmc = (MappingJackson2HttpMessageConverter) hmc;
mj2hmc.setObjectMapper(objectMapper);
}
}
return (RestOperations) Proxy.newProxyInstance(RestOperations.class.getClassLoader(),
new Class[]{RestOperations.class},
new RetryableRestOperationsHandler(restTemplate, retryTimes, retryPolicyFactory));
}
示例2: doRead
import org.springframework.web.client.RestOperations; //导入依赖的package包/类
private <T> T doRead(final String path, final Class<T> responseType) {
return doWithSession(new RestOperationsCallback<T>() {
@Override
public T doWithRestOperations(RestOperations restOperations) {
try {
return restOperations.getForObject(path, responseType);
} catch (HttpStatusCodeException e) {
if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
return null;
}
throw VaultResponses.buildException(e, path);
}
}
});
}
示例3: create
import org.springframework.web.client.RestOperations; //导入依赖的package包/类
@Override
public CredentialProvider create(final SocialProperties properties) {
final OAuth2ConnectorProperties oauth2Properties = (OAuth2ConnectorProperties) properties;
final String appId = oauth2Properties.getAppId();
final String appSecret = oauth2Properties.getAppSecret();
final String authorizationUrl = oauth2Properties.getAuthorizationUrl();
final String authenticationUrl = oauth2Properties.getAuthenticationUrl();
final String accessTokenUrl = oauth2Properties.getAccessTokenUrl();
final boolean useParametersForClientCredentials = oauth2Properties.isUseParametersForClientCredentials();
final TokenStrategy tokenStrategy = oauth2Properties.getTokenStrategy();
final String scope = oauth2Properties.getScope();
final OAuth2ServiceProvider<RestOperations> serviceProvider = new GenericOAuth2ServiceProvider(appId, appSecret, authorizationUrl,
authenticationUrl, accessTokenUrl, useParametersForClientCredentials, tokenStrategy);
final OAuth2ConnectionFactory<RestOperations> connectionFactory = new OAuth2ConnectionFactory<>("oauth2", serviceProvider, null);
connectionFactory.setScope(scope);
final OAuth2Applicator applicator = new OAuth2Applicator(properties);
applicator.setAccessTokenProperty("accessToken");
return new OAuth2CredentialProvider<>("oauth2", connectionFactory, applicator);
}
示例4: write
import org.springframework.web.client.RestOperations; //导入依赖的package包/类
@Override
public <T> CredentialDetails<T> write(final CredentialRequest<T> credentialRequest) {
Assert.notNull(credentialRequest, "credentialRequest must not be null");
final ParameterizedTypeReference<CredentialDetails<T>> ref =
new ParameterizedTypeReference<CredentialDetails<T>>() {};
return doWithRest(new RestOperationsCallback<CredentialDetails<T>>() {
@Override
public CredentialDetails<T> doWithRestOperations(RestOperations restOperations) {
ResponseEntity<CredentialDetails<T>> response =
restOperations.exchange(BASE_URL_PATH, PUT,
new HttpEntity<CredentialRequest<T>>(credentialRequest), ref);
throwExceptionOnError(response);
return response.getBody();
}
});
}
示例5: generate
import org.springframework.web.client.RestOperations; //导入依赖的package包/类
@Override
public <T, P> CredentialDetails<T> generate(final ParametersRequest<P> parametersRequest) {
Assert.notNull(parametersRequest, "parametersRequest must not be null");
final ParameterizedTypeReference<CredentialDetails<T>> ref =
new ParameterizedTypeReference<CredentialDetails<T>>() {};
return doWithRest(new RestOperationsCallback<CredentialDetails<T>>() {
@Override
public CredentialDetails<T> doWithRestOperations(RestOperations restOperations) {
ResponseEntity<CredentialDetails<T>> response =
restOperations.exchange(BASE_URL_PATH, POST,
new HttpEntity<ParametersRequest<P>>(parametersRequest), ref);
throwExceptionOnError(response);
return response.getBody();
}
});
}
示例6: regenerate
import org.springframework.web.client.RestOperations; //导入依赖的package包/类
@Override
public <T> CredentialDetails<T> regenerate(final CredentialName name) {
Assert.notNull(name, "credential name must not be null");
final ParameterizedTypeReference<CredentialDetails<T>> ref =
new ParameterizedTypeReference<CredentialDetails<T>>() {};
return doWithRest(new RestOperationsCallback<CredentialDetails<T>>() {
@Override
public CredentialDetails<T> doWithRestOperations(RestOperations restOperations) {
Map<String, Object> request = new HashMap<String, Object>(1);
request.put("name", name.getName());
ResponseEntity<CredentialDetails<T>> response =
restOperations.exchange(REGENERATE_URL_PATH, POST,
new HttpEntity<Map<String, Object>>(request), ref);
throwExceptionOnError(response);
return response.getBody();
}
});
}
示例7: getById
import org.springframework.web.client.RestOperations; //导入依赖的package包/类
@Override
public <T> CredentialDetails<T> getById(final String id, Class<T> credentialType) {
Assert.notNull(id, "credential id must not be null");
Assert.notNull(credentialType, "credential type must not be null");
final ParameterizedTypeReference<CredentialDetails<T>> ref =
new ParameterizedTypeReference<CredentialDetails<T>>() {};
return doWithRest(new RestOperationsCallback<CredentialDetails<T>>() {
@Override
public CredentialDetails<T> doWithRestOperations(RestOperations restOperations) {
ResponseEntity<CredentialDetails<T>> response =
restOperations.exchange(ID_URL_PATH, GET, null, ref, id);
throwExceptionOnError(response);
return response.getBody();
}
});
}
示例8: getByName
import org.springframework.web.client.RestOperations; //导入依赖的package包/类
@Override
public <T> CredentialDetails<T> getByName(final CredentialName name, Class<T> credentialType) {
Assert.notNull(name, "credential name must not be null");
Assert.notNull(credentialType, "credential type must not be null");
final ParameterizedTypeReference<CredentialDetailsData<T>> ref =
new ParameterizedTypeReference<CredentialDetailsData<T>>() {};
return doWithRest(new RestOperationsCallback<CredentialDetails<T>>() {
@Override
public CredentialDetails<T> doWithRestOperations(RestOperations restOperations) {
ResponseEntity<CredentialDetailsData<T>> response =
restOperations.exchange(NAME_URL_QUERY_CURRENT, GET, null, ref, name.getName());
throwExceptionOnError(response);
return response.getBody().getData().get(0);
}
});
}
示例9: getByNameWithHistory
import org.springframework.web.client.RestOperations; //导入依赖的package包/类
@Override
public <T> List<CredentialDetails<T>> getByNameWithHistory(final CredentialName name, Class<T> credentialType) {
Assert.notNull(name, "credential name must not be null");
Assert.notNull(credentialType, "credential type must not be null");
final ParameterizedTypeReference<CredentialDetailsData<T>> ref =
new ParameterizedTypeReference<CredentialDetailsData<T>>() {};
return doWithRest(new RestOperationsCallback<List<CredentialDetails<T>>>() {
@Override
public List<CredentialDetails<T>> doWithRestOperations(RestOperations restOperations) {
ResponseEntity<CredentialDetailsData<T>> response =
restOperations.exchange(NAME_URL_QUERY, GET, null, ref, name.getName());
throwExceptionOnError(response);
return response.getBody().getData();
}
});
}
示例10: findByName
import org.springframework.web.client.RestOperations; //导入依赖的package包/类
@Override
public List<CredentialSummary> findByName(final CredentialName name) {
Assert.notNull(name, "credential name must not be null");
return doWithRest(new RestOperationsCallback<List<CredentialSummary>>() {
@Override
public List<CredentialSummary> doWithRestOperations(
RestOperations restOperations) {
ResponseEntity<CredentialSummaryData> response = restOperations
.getForEntity(NAME_LIKE_URL_QUERY,
CredentialSummaryData.class, name.getName());
throwExceptionOnError(response);
return response.getBody().getCredentials();
}
});
}
示例11: findByPath
import org.springframework.web.client.RestOperations; //导入依赖的package包/类
@Override
public List<CredentialSummary> findByPath(final String path) {
Assert.notNull(path, "credential path must not be null");
return doWithRest(new RestOperationsCallback<List<CredentialSummary>>() {
@Override
public List<CredentialSummary> doWithRestOperations(
RestOperations restOperations) {
ResponseEntity<CredentialSummaryData> response = restOperations
.getForEntity(PATH_URL_QUERY, CredentialSummaryData.class,
path);
throwExceptionOnError(response);
return response.getBody().getCredentials();
}
});
}
示例12: addPermissions
import org.springframework.web.client.RestOperations; //导入依赖的package包/类
@Override
public List<CredentialPermission> addPermissions(final CredentialName name, CredentialPermission... permissions) {
Assert.notNull(name, "credential name must not be null");
final CredentialPermissions credentialPermissions = new CredentialPermissions(name, permissions);
return doWithRest(new RestOperationsCallback<List<CredentialPermission>>() {
@Override
public List<CredentialPermission> doWithRestOperations(RestOperations restOperations) {
ResponseEntity<CredentialPermissions> response =
restOperations.exchange(PERMISSIONS_URL_PATH, POST,
new HttpEntity<CredentialPermissions>(credentialPermissions),
CredentialPermissions.class);
return response.getBody().getPermissions();
}
});
}
示例13: interpolateServiceData
import org.springframework.web.client.RestOperations; //导入依赖的package包/类
@Override
public ServicesData interpolateServiceData(final ServicesData serviceData) {
Assert.notNull(serviceData, "serviceData must not be null");
return doWithRest(new RestOperationsCallback<ServicesData>() {
@Override
public ServicesData doWithRestOperations(RestOperations restOperations) {
ResponseEntity<ServicesData> response = restOperations
.exchange(INTERPOLATE_URL_PATH, POST,
new HttpEntity<ServicesData>(serviceData), ServicesData.class);
throwExceptionOnError(response);
return response.getBody();
}
});
}
示例14: retryableRestOperationsFailTest
import org.springframework.web.client.RestOperations; //导入依赖的package包/类
@Test
public void retryableRestOperationsFailTest() {
ctx.close();
int retryTimes = 10;
RetryPolicyFactory mockedRetryPolicyFactory = Mockito.mock(RetryPolicyFactory.class);
RetryPolicy mockedRetryPolicy = Mockito.mock(RetryPolicy.class);
when(mockedRetryPolicyFactory.create()).thenReturn(mockedRetryPolicy);
when(mockedRetryPolicy.retry(any(Throwable.class))).thenReturn(true);
RestOperations restOperations = RestTemplateFactory.createCommonsHttpRestTemplate(10, 100, 5000, 5000,
retryTimes, mockedRetryPolicyFactory);
try {
restOperations.getForObject(generateRequestURL("/test"), String.class);
} catch (Exception e) {
verify(mockedRetryPolicy, times(retryTimes)).retry(any(Throwable.class));
// check the type of original exception
assertTrue(e instanceof ResourceAccessException);
}
}
示例15: lookupSelf
import org.springframework.web.client.RestOperations; //导入依赖的package包/类
private static Map<String, Object> lookupSelf(RestOperations restOperations,
VaultToken token) {
try {
ResponseEntity<VaultResponse> entity = restOperations.exchange(
"auth/token/lookup-self", HttpMethod.GET, new HttpEntity<>(
VaultHttpHeaders.from(token)), VaultResponse.class);
Assert.state(entity.getBody() != null && entity.getBody().getData() != null,
"Token response is null");
return entity.getBody().getData();
}
catch (HttpStatusCodeException e) {
throw new VaultTokenLookupException(String.format(
"Token self-lookup failed: %s %s", e.getStatusCode(),
VaultResponses.getError(e.getResponseBodyAsString())));
}
}