我使用Rest Template创建了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);
但即使在给予依赖之后,构建也失败了。 任何人都可以帮我吗?
答案 0 :(得分:0)
我不确定为什么依赖不能解决,但我可以告诉你我是如何解决这个问题的。
首先,我为所有其他模板功能提供服务:
@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和Entity - 顺便说一下,我使用小型构建器来处理各种参数图...
... paramsMap = anParametersMap().withEntry("Authorization", encodeToBase64(":", usr, pass)).build()
restTemplateService.doPOSTRequestForObject("rest webservice url",requestBody, XXX.Class, null, paramsMap, null);
顺便说一句,你可以通过向restTemplate拦截器添加BasicAuthorizationInterceptor来完成所有这些,但我没有采用它。
答案 1 :(得分:0)
Hybris默认情况下不会下载传递依赖项,因此不会下载所有必需的库(依赖项的依赖项)。您必须全部指定它们或启用下载传递依赖项。在此处阅读如何操作:hybris's maven doesn't download transitive dependencies。
答案 2 :(得分:-1)
您可以使用RestTemplate
创建RestTemplateBuilder
,如下所示。
@Service
public class ApiClient {
private RestTemplate restTemplate;
public ApiClient(RestTemplateBuilder builder) {
restTemplate = builder.basicAuthorization("username", "password").build();
}
//Other implementation details
}
然后可以使用此RestTemplate
实例调用远程api。