首页 > 解决方案 > 构建时 RestTemplateBuilder 失败

问题描述

我使用 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);

但即使在给予依赖之后,构建也失败了。谁可以帮我这个事?

标签: javaspringspring-boothybris

解决方案


我不确定为什么依赖没有解决,但我可以告诉你我是如何解决问题的。

首先,我为我所有的其余模板功能提供服务:

@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);

顺便说一句,您可以通过将 BasicAuthorizationInterceptor 添加到 restTemplate 拦截器来完成所有这些,但我没有这样做。


推荐阅读