我正在嘗試使用以下代碼在我的Spring Rest應用程序中擊中一個Microsoft Flow POST URL,但它給了我401個錯誤。 我的代碼:當使用授權標頭髮送時,Spring RestTemplate會給出401未授權錯誤
@RequestMapping(value = "/hookslistner", method = RequestMethod.POST)
public ResponseEntity<Void> recieveWebhook(@RequestBody InventorySystemModel inventory,
@RequestHeader("event") String event,
@RequestHeader("Authorization") String authorization) {
// authorization = "Basic <Base64 encoded value of username:pwd>"
RestTemplate restTemplate = new RestTemplate();
org.springframework.http.HttpHeaders httpHeaders = new org.springframework.http.HttpHeaders();
String url = "https://prod-01.centralindia.logic.azure.com/workflows/835348<hiding rest of part>";
String headerName = "Authorization";
httpHeaders.add(headerName, authorization);
httpHeaders.add("Content-Type", "application/json");
HttpEntity<String> requestEntity = new HttpEntity<>("Headers", httpHeaders);
System.out.println(">>>>>>>" + restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class).getBody());
}
錯誤:
SEVERE: Servlet.service() for servlet [webhooks] in context with path [/inventoryhooks] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 401 Unauthorized] with root cause
org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
.
.
.
是不是因爲我的目標URL爲https和我的本地是http?
任何人都可以指出我哪裏出了問題?
此時被拋出的異常?在RestTemplate或進入方法之前? –
at'restTemplate.exchange' –