示例Spring RestTemplate实现

问题描述 投票:1回答:1

我想从我的本地spring应用程序将一些存档文件推送到GITHub。

repos/{:user}/{:repo}/git/refs/heads/{:branchname}

{
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("XXXXXX", 8080));
    requestFactory.setProxy(proxy);
    RestTemplate restTemplate = new RestTemplate(requestFactory);
    String url = new String("https://api.github.com/repos/XXX/MyApplication/git/refs/heads/XXX");
    Map<String, String> req = new HashMap<String,String>();
    req.put("sha", "9a7fd370e28ea7a4bc8242e7f234s5ed07042cb88");
    String jsonObject = JSONParser.quote(payload);
    HttpEntity<Object> httpEntity = new HttpEntity<>(headers1);
   restTemplate.exchange(url, HttpMethod.POST,httpEntity,Object.class);
}

Error: Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 404 Not Found
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:63)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531)

使用下面的POST API,我得到'404 not found error'。

Method : POST
URL : https://api.github.com/repos/{:user}/{:repo}/git/refs/heads/{:branchname}
{
  "sha": "{:new-commit-sha}"
}

你能为上面的API提供Spring RestTemplate实例吗?

spring spring-boot github resttemplate
1个回答
1
投票

我在上面的场景中发现了问题。对于上面的POST客户端userId和密码在Headers中它是错误的。

我们需要在BasicAutherizationInceptors类中提供userId和Password,如下所示。

resttemplate.getInterceptors().add(new BasicAutherizationInterceptor(userId,Pwd));
© www.soinside.com 2019 - 2024. All rights reserved.