azure logic 应用程序请求触发器 url 无法在 RestTemplate java 中工作,但在邮递员中工作
azure logic app request trigger url is not working from RestTemplate java but working in postman
我有带有请求触发器的 Azure 逻辑应用程序。我想从我的 java 应用程序触发这个逻辑应用程序。所以,我试图从我的 java API.
调用请求触发器 url
如果我在邮递员中点击,它工作正常,
logic app with request trigger hitting via postman
但是在 java.
中使用 RestTemplate 调用它时得到 401
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
@RestController
public class SampleController {
@RequestMapping(value="/display",method = RequestMethod.GET)
public ResponseEntity<String> display()
{
System.out.println("entered");
RestTemplate restTemplate = new RestTemplate();
String url = "https://pt-38.northeurope.logic.azure.com/workflows/cc0363a3bf134efca43b0bd2d15d5ed5/triggers/manual/paths/invoke?api-version=2016-10-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=HnllGcN9xo91kRGAjk";
HttpHeaders headers = new HttpHeaders();
headers.set("api-version", "2016-10-01");
headers.set("sp", "%2Ftriggers%2Fmanual%2Frun");
headers.set("sv", "1.0");
headers.set("sig", "HnllGcN9xo91kRGAjk3Zlp6fW0dhwf");
HttpEntity request = new HttpEntity(headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
// JSONObject obj = new JSONObject(response.getBody());
// System.out.println("obj");
return response;
}
}
logic app with request trigger accessing via rest template
根据 Hury Shen 的建议,我更改了 api 代码如下,
public HttpEntity<String> display1()
{
System.out.println("entered");
RestTemplate restTemplate = new RestTemplate();
String url = "https://prod-38.northeurope.logic.azure.com/workflows/cc0363a3bf134efca43b0bd2d15d5ed5/triggers/manual/paths/invoke?api-version=2016-10-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=HnllGcN9xo91kRGAjk3Zlp6fW0dhwfaRij-fk1CC60c";
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
.queryParam("api-version", "2016-10-01")
.queryParam("sp", "%2Ftriggers%2Fmanual%2Frun")
.queryParam("sv", "1.0")
.queryParam("sig", "HnllGcN9xo91kRGAjk3Zlp6fW0dhwfaRij-fk1CC60c");
HttpEntity<?> entity = new HttpEntity<>(headers);
HttpEntity<String> response = restTemplate.exchange(
builder.toUriString(),
HttpMethod.GET,
entity,
String.class);
return response;
}
低于异常,
对于这个问题,我们应该将api-version
、sp
、sv
和sig
的值放在查询参数中,而不是放在headers中。
这样做:
Map<String, String> params = new HashMap<String, String>();
params.put("api-version", "xxxx");
params.put("sp", "xxxxx");
params.put("sv", "xxx");
params.put("sig", "xxxxxxx");
.....
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class, params);
我有带有请求触发器的 Azure 逻辑应用程序。我想从我的 java 应用程序触发这个逻辑应用程序。所以,我试图从我的 java API.
调用请求触发器 url如果我在邮递员中点击,它工作正常, logic app with request trigger hitting via postman
但是在 java.
中使用 RestTemplate 调用它时得到 401import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
@RestController
public class SampleController {
@RequestMapping(value="/display",method = RequestMethod.GET)
public ResponseEntity<String> display()
{
System.out.println("entered");
RestTemplate restTemplate = new RestTemplate();
String url = "https://pt-38.northeurope.logic.azure.com/workflows/cc0363a3bf134efca43b0bd2d15d5ed5/triggers/manual/paths/invoke?api-version=2016-10-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=HnllGcN9xo91kRGAjk";
HttpHeaders headers = new HttpHeaders();
headers.set("api-version", "2016-10-01");
headers.set("sp", "%2Ftriggers%2Fmanual%2Frun");
headers.set("sv", "1.0");
headers.set("sig", "HnllGcN9xo91kRGAjk3Zlp6fW0dhwf");
HttpEntity request = new HttpEntity(headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
// JSONObject obj = new JSONObject(response.getBody());
// System.out.println("obj");
return response;
}
}
logic app with request trigger accessing via rest template
根据 Hury Shen 的建议,我更改了 api 代码如下,
public HttpEntity<String> display1()
{
System.out.println("entered");
RestTemplate restTemplate = new RestTemplate();
String url = "https://prod-38.northeurope.logic.azure.com/workflows/cc0363a3bf134efca43b0bd2d15d5ed5/triggers/manual/paths/invoke?api-version=2016-10-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=HnllGcN9xo91kRGAjk3Zlp6fW0dhwfaRij-fk1CC60c";
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
.queryParam("api-version", "2016-10-01")
.queryParam("sp", "%2Ftriggers%2Fmanual%2Frun")
.queryParam("sv", "1.0")
.queryParam("sig", "HnllGcN9xo91kRGAjk3Zlp6fW0dhwfaRij-fk1CC60c");
HttpEntity<?> entity = new HttpEntity<>(headers);
HttpEntity<String> response = restTemplate.exchange(
builder.toUriString(),
HttpMethod.GET,
entity,
String.class);
return response;
}
低于异常,
对于这个问题,我们应该将api-version
、sp
、sv
和sig
的值放在查询参数中,而不是放在headers中。
这样做:
Map<String, String> params = new HashMap<String, String>();
params.put("api-version", "xxxx");
params.put("sp", "xxxxx");
params.put("sv", "xxx");
params.put("sig", "xxxxxxx");
.....
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class, params);