我使用Rest模板创建Rest Web服务调用,对于基本身份验证,我尝试使用RestTemplateBuilder在发送请求时构建基本身份验证。我给出了所有的spring boot依赖项:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
代码:
final RestTemplate restTemplate = restTemplateBuilder
.basicAuthorization("username", "password").build();
restTemplate.postForObject("rest webservice url",inputparam, XXX.Class);
但是,即使在提供了依赖项之后,构建也会失败。有人能在这方面帮我吗?
发布于 2018-06-08 16:01:21
我不确定为什么没有解决依赖关系,但我可以告诉你我是如何解决这个问题的。
首先,我为所有的rest模板功能提供了服务:
@Service
@SuppressWarnings("squid:RedundantThrowsDeclarationCheck")
public class RestTemplateService {
private RestTemplate restTemplate;
public RestTemplateService() {
this.restTemplate = new RestTemplate();
}
public <T> T doGETRequestForObject(String url, Class<T> responseType) throws RestClientException {
return restTemplate.getForObject(url, responseType);
}
public <T> T doGETRequestForObject(String url, Class<T> responseType, Map<String, String> queryParams) throws RestClientException {
return restTemplate.getForObject(url, responseType, queryParams);
}
public <T> T doGETRequestForObject(String url, Class<T> responseType, Map<String, String> queryParams, Map<String, String> headerParams, MediaType... mediaTypes) throws RequestIsNotOKException {
return doGETRequestForEntity(url, responseType, queryParams, headerParams, mediaTypes).getBody();
}
public <T> ResponseEntity<T> doGETRequestForEntity(String url, Class<T> responseType) throws RestClientException, RequestIsNotOKException {
ResponseEntity<T> entity = restTemplate.getForEntity(url, responseType);
validateResponseStatus(entity);
return entity;
}
public <T> ResponseEntity<T> doGETRequestForEntity(String url, Class<T> responseType, Map<String, String> queryParams) throws RestClientException, RequestIsNotOKException {
ResponseEntity<T> entity = restTemplate.getForEntity(url, responseType, queryParams);
validateResponseStatus(entity);
return entity;
}
public <T> ResponseEntity<T> doGETRequestForEntity(String url, Class<T> responseType, Map<String, String> queryParams, Map<String, String> headerParams, MediaType... mediaTypes) throws RestClientException, RequestIsNotOKException {
ResponseEntity<T> entity = restTemplate.exchange(url, HttpMethod.GET, getHttpEntity(headerParams, mediaTypes, null), responseType, queryParams);
validateResponseStatus(entity);
return entity;
}
public HttpStatus doPOSTRequestForStatusCode(String url, Object requestBody) throws RestClientException {
return restTemplate.postForEntity(url, requestBody, Void.class).getStatusCode();
}
public HttpStatus doPOSTRequestForStatusCode(String url, Object requestBody, Map<String, String> queryParams) throws RestClientException {
return restTemplate.postForEntity(url, requestBody, Void.class, queryParams).getStatusCode();
}
public <T> T doPOSTRequestForObject(String url, Object requestBody, Class<T> responseType) throws RestClientException, RequestIsNotOKException {
return restTemplate.postForObject(url, requestBody, responseType);
}
public <T> T doPOSTRequestForObject(String url, Object requestBody, Class<T> responseType, Map<String, String> queryParams) throws RestClientException {
return restTemplate.postForObject(url, requestBody, responseType, queryParams);
}
public <T> T doPOSTRequestForObject(String url, Object requestBody, Class<T> responseType, Map<String, String> queryParams, Map<String, String> headerParams, MediaType... mediaTypes) throws RestClientException, RequestIsNotOKException {
return doPOSTRequestForEntity(url, requestBody, responseType, queryParams, headerParams, mediaTypes).getBody();
}
public <T> ResponseEntity<T> doPOSTRequestForEntity(String url, Object requestBody, Class<T> responseType) throws RestClientException, RequestIsNotOKException {
ResponseEntity<T> entity = restTemplate.postForEntity(url, requestBody, responseType);
validateResponseStatus(entity);
return entity;
}
public <T> ResponseEntity<T> doPOSTRequestForEntity(String url, Object requestBody, Class<T> responseType, Map<String, String> queryParams) throws RestClientException, RequestIsNotOKException {
ResponseEntity<T> entity = restTemplate.postForEntity(url, requestBody, responseType, queryParams);
validateResponseStatus(entity);
return entity;
}
public <T> ResponseEntity<T> doPOSTRequestForEntity(String url, Object requestBody, Class<T> responseType, Map<String, String> queryParams, Map<String, String> headerParams, MediaType... mediaTypes) throws RestClientException, RequestIsNotOKException {
ResponseEntity<T> entity = restTemplate.exchange(url, HttpMethod.POST, getHttpEntity(headerParams, mediaTypes, requestBody), responseType, queryParams);
validateResponseStatus(entity);
return entity;
}
private HttpEntity getHttpEntity(Map<String, String> headerParams, MediaType[] mediaTypes, Object requestBody) {
return aHttpEntity()
.withHeaderParams(headerParams)
.withMediaTypes(mediaTypes)
.withBody(requestBody)
.build();
}
public <T> void validateResponseStatus(ResponseEntity<T> entity) throws RequestIsNotOKException {
if (HttpStatus.OK.equals(entity.getStatusCode())) {
throw new RequestIsNotOKException(entity);
}
}
public RestTemplate getRestTemplate() {
return restTemplate;
}
public static class RequestIsNotOKException extends Exception {
private final ResponseEntity entity;
public RequestIsNotOKException(ResponseEntity entity) {
this.entity = entity;
}
public ResponseEntity getEntity() {
return entity;
}
}
}
然后,如果我想通过基本身份验证,我可以使用util类中的静态方法进行编码
public static String encodeToBase64(String separator, String... parameters) {
validateNonNulls(parameters);
if (isBlank(separator)) {
separator = StringUtils.EMPTY;
}
return encodeToBase64(join(parameters, separator));
}
public static String encodeToBase64(String strToEncode) {
byte[] encodedStr = Base64.encodeBase64(strToEncode.getBytes());
return new String(encodedStr);
}
现在您可以将已编码的usr/pass传递给服务方法,该方法将构建HttpHeader和实体--顺便说一句,我使用小型构建器来创建各种类型的参数映射……
... paramsMap = anParametersMap().withEntry("Authorization", encodeToBase64(":", usr, pass)).build()
restTemplateService.doPOSTRequestForObject("rest webservice url",requestBody, XXX.Class, null, paramsMap, null);
顺便说一句,您可以通过向restTemplate拦截器添加BasicAuthorizationInterceptor来完成所有这些任务,但我并没有这么做。
发布于 2018-06-10 05:47:33
默认情况下,Hybris不会下载可传递依赖项,因此并不是所有必需的库都会下载(依赖项的依赖项)。您必须将它们全部指定或启用下载传递依赖项。在这里阅读如何做:hybris's maven doesn't download transitive dependencies。
发布于 2018-06-08 14:31:18
您可以使用RestTemplateBuilder
创建RestTemplate
,如下所示。
@Service
public class ApiClient {
private RestTemplate restTemplate;
public ApiClient(RestTemplateBuilder builder) {
restTemplate = builder.basicAuthorization("username", "password").build();
}
//Other implementation details
}
然后可以使用此RestTemplate
实例调用远程api。
https://stackoverflow.com/questions/50748358
复制相似问题