Я не уверен, почему зависимость не разрешается, но я могу рассказать вам, как я решил эту проблему.
Во-первых, у меня есть служба для всех остальных функций моего шаблона:
@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);
Кстати, вы можете сделать все это, добавив BasicAuthorizationInterceptor для перехватчиков restTemplate, но я не сделалне пойду с этим.