在AD上创建用户时,我试图从图形api中获取错误响应,但无法捕获来自201以外的API的响应,这是成功创建的用户。
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON }));
headers.set("Authorization", "Bearer " + token);
headers.set("Content-Type", "application/json");
try{
HttpEntity<String> request = new HttpEntity<String>(jsonRequest.toString(), headers);
responseEntity = restTemplate.postForEntity(graphUrl, request, String.class);
}catch(Exception e){
System.out.println("Result - status ("+ responseEntity.getStatusCode() + ") has body: " + responseEntity.hasBody());
}
只要响应不是201,它就会向我显示一个异常Bad Request 400
我收到邮递员的错误响应,但在Java应用中,使用相同的有效负载,响应为空
答案 0 :(得分:1)
默认情况下,Spring的RestTemplate
和Spring Boot的TestRestTemplate
将在JDK的内部HttpURLConnection
实现中,该实现无法访问状态为401“未授权”的HTTP响应的正文。您可以使用没有问题的HTTPClient。
您可以参考以下工作组:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version> <scope>test</scope>
</dependency>
String token="eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Im5iQ3dXMTF3M1hrQi14VWFYd0tSU0xqTUhHUSIsImtpZCI6Im5iQ3dXMTF3M1hrQi14VWFYd0tSU0xqTUhHUSJ9.eyJhdWQiOiJodHRwczovL21hbmFnZW1lbnQuYXp1cmUuY29tLyIsImlzcyI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0L2U0YzlhYjRlLWJkMjctNDBkNS04NDU5LTIzMGJhMmE3NTdmYi8iLCJpYXQiOjE1NDY1NzkxNTgsIm5iZiI6MTU0NjU3OTE1OCwiZXhwIjoxNTQ2NTgzMDU4LCJhaW8iOiI0MlJnWU5qSG45NTBZV0p5dXIvZXozL0trNm9iQUE9PSIsImFwcGlkIjoiNjNkMzk0ZDctYjA1OC00MDRkLWJjYjMtMThmODhmN2UzY2FhIiwiYXBwaWRhY3IiOiIxIiwiaWRwIjoiaHR0cHM6Ly9zdHMud2luZG93cy5uZXQvZTRjOWFiNGUtYmQyNy00MGQ1LTg0NTktMjMwYmEyYTc1N2ZiLyIsIm9pZCI6ImZiZmVkNzhiLWNkYWUtNDU3NC05YzM4LTY0MjlmMjI2MTMyOCIsInN1YiI6ImZiZmVkNzhiLWNkYWUtNDU3NC05YzM4LTY0MjlmMjI2MTMyOCIsInRpZCI6ImU0YzlhYjRlLWJkMjctNDBkNS04NDU5LTIzMGJhMmE3NTdmYiIsInV0aSI6IlVTRzMzM3B3Q2tDc3MxN3ZRdVFDQUEiLCJ2ZXIiOiIxLjAifQ.cFfptQOBt_BQKAbOSFsewmAMsgku15G8oKrfTuxD8ID2AZYtIWqEt1kFXKYBb66Hmsephe_KgyyeByIY_RLPW7JT3cpMTiSkh1DsiRvdAJJkZ0fcmgWQsZB-mmsS-Kt5vTUO1F5EtNeneaaklexfuEMG9E_hUiPKM2v46LlD3pO9ZPlq45157nUMcxPJlPpeHoE3riXwBEUQTCUFTm60NHOG1Cs_NuC_bVHBC3oN-pCFabz-vMo4O2Zikxn6S-BYxz909EP5lAmRNjKgH3uPYHS5rWfve20NiUPy8nOyBHKgz6XxAJXz7PVxm_hTOtdsGC9Hg8bdxnOKctPo95vAlQ";
String jsonRequest="{\"test\":\"a\"}";
String graphUrl="https://graph.microsoft.com/v1.0/users";
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(graphUrl);
postMethod.setRequestHeader("Content-Type", "application/json");
postMethod.setRequestHeader("Authorization", "Bearer " + token);
postMethod.setRequestBody(jsonRequest);
try {
int code = client.executeMethod(postMethod);
String res = postMethod.getResponseBodyAsString();
System.out.print(res);
} catch (IOException e) {
e.printStackTrace();
}
您还可以参考其他信息here。
答案 1 :(得分:0)
StringEntity postStr = new StringEntity(jsonRequest);
CloseableHttpClient client = HttpClients.createDefault();
HttpPost postM = new HttpPost(graphUrl);
postM.setHeader("Content-Type", "application/json");
postM.setHeader("Authorization", "Bearer " +token);
postM.setEntity(postStr);
try {
CloseableHttpResponse response = client.execute(postMethod);
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode == 201){
String res = EntityUtils.toString(response.getEntity());
}
上面的代码为我工作。