首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >构建时RestTemplateBuilder失败

构建时RestTemplateBuilder失败
EN

Stack Overflow用户
提问于 2018-06-08 02:52:39
回答 3查看 1.5K关注 0票数 0

我使用Rest模板创建Rest Web服务调用,对于基本身份验证,我尝试使用RestTemplateBuilder在发送请求时构建基本身份验证。我给出了所有的spring boot依赖项:

代码语言:javascript
复制
<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>

代码:

代码语言:javascript
复制
final RestTemplate restTemplate = restTemplateBuilder
      .basicAuthorization("username", "password").build();
restTemplate.postForObject("rest webservice url",inputparam, XXX.Class);

但是,即使在提供了依赖项之后,构建也会失败。有人能在这方面帮我吗?

EN

回答 3

Stack Overflow用户

发布于 2018-06-08 16:01:21

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

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

代码语言:javascript
复制
@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类中的静态方法进行编码

代码语言:javascript
复制
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和实体--顺便说一句,我使用小型构建器来创建各种类型的参数映射……

代码语言:javascript
复制
... paramsMap = anParametersMap().withEntry("Authorization", encodeToBase64(":", usr, pass)).build()

restTemplateService.doPOSTRequestForObject("rest webservice url",requestBody, XXX.Class, null, paramsMap, null);

顺便说一句,您可以通过向restTemplate拦截器添加BasicAuthorizationInterceptor来完成所有这些任务,但我并没有这么做。

票数 0
EN

Stack Overflow用户

发布于 2018-06-10 05:47:33

默认情况下,Hybris不会下载可传递依赖项,因此并不是所有必需的库都会下载(依赖项的依赖项)。您必须将它们全部指定或启用下载传递依赖项。在这里阅读如何做:hybris's maven doesn't download transitive dependencies

票数 0
EN

Stack Overflow用户

发布于 2018-06-08 14:31:18

您可以使用RestTemplateBuilder创建RestTemplate,如下所示。

代码语言:javascript
复制
@Service
public class ApiClient {

    private RestTemplate restTemplate;

    public ApiClient(RestTemplateBuilder builder) {
        restTemplate = builder.basicAuthorization("username", "password").build();
    }

    //Other implementation details
}

然后可以使用此RestTemplate实例调用远程api。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50748358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档