使用Rest Template的管道调用失败

时间:2019-03-04 03:13:25

标签: java spring spring-boot spring-rest go-cd

我有一种情况,我想使用我的spring-boot服务暂停pineline。我正在使用Spring-boot和Go Pipeline API 16.1.0

以下是GO API文档中提供的实际URL,可以通过运行良好的CURL暂停管道

  

卷曲'http://ci.example.com/go/api/pipelines/dev1-pineline/pause'-u   'username:password'-X POST -d'pauseCause = testing Pause'

上面的CURL URL正常工作,但是当我尝试使用RestTemplate做同样的事情时,它不工作。 RestTemplate功能代码为

public Object pausePipeline() {

  String credentials = userName+":"+password;
  byte[] encoding = Base64.encodeBase64(credentials.getBytes());
  HttpHeaders headers = new HttpHeaders();
  headers.set("Authorization", String.valueOf(encoding));
  //headers.setContentType(MediaType.APPLICATION_JSON); // optional

  String url = String.format("%s/%s%s", pipelineUrl,pipelineName,"/pause");
  String data = "pauseCause=Monthend process started.";

  HttpEntity<String> entity = new HttpEntity<>(data, headers);
  return restTemplate.exchange(url, HttpMethod.POST, entity , Object.class).getBody();
}

上面的代码在尝试使用Rest端点异常堆栈跟踪时引发异常

    [o.a.c.c.C.[.[.[.[dispatcherServlet]] (http-nio-8080-exec-1) Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 401 Full authentication is required to access this resource] with root cause
org.springframework.web.client.HttpClientErrorException: 401 Full authentication is required to access this resource
       at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
       at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:614) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
       at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:570) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
       at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:530) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
       at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:448) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
       at com.premierinc.forecasting.suggesteds.MonthEndService.pausePipeline(MonthEndService.java:51) ~[main/:?]
       at com.premierinc.forecasting.web.ExtractsApi.pauseMonendPipeline(ExtractsApi.java:104) ~[main/:?]
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_181]
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_181]
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_181]
       at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_181]
       at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1270) ~[springloaded-1.2.3.RELEASE.jar:1.2.3.RELEASE]
       at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) ~[seb-4.1.6.RELEASE.jar:4.1.6.RELEASE]

1 个答案:

答案 0 :(得分:0)

您可能想在"photoSet${this._step}"标头前面加上Basic。您代码中的相关更改可能与以下内容类似

Authorization

参考-https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization#Examples