Java 417使用RestTemplate执行POST请求时预期失败
我需要使用RestTemplate创建一个POST请求到身份验证端点以获取令牌。端点需要一个原始JSON,其外观如下所示:Java 417使用RestTemplate执行POST请求时预期失败,java,spring,http,exception,Java,Spring,Http,Exception,我需要使用RestTemplate创建一个POST请求到身份验证端点以获取令牌。端点需要一个原始JSON,其外观如下所示: { "client_id" : "12345" "client_secret" : "12345" } 我尝试将json作为映射添加到HttpEntity中,同时添加hedaer中的应用程序/json内容类型。原始JSON是我唯一需要的东西 String url = "https://oauth/token"; RestTemplate res
{
"client_id" : "12345"
"client_secret" : "12345"
}
我尝试将json作为映射添加到HttpEntity中,同时添加hedaer中的应用程序/json
内容类型。原始JSON是我唯一需要的东西
String url = "https://oauth/token";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
MultiValueMap<String,String> map = new LinkedMultiValueMap<>();
map.add("client_id", "b890" );
map.add("client_secret", "576a0");
HttpEntity<MultiValueMap<String,String>> request = new HttpEntity<>(map, headers);
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
assert(response.hasBody());
ObjectMapper mapper = new ObjectMapper();
System.out.println(response.getBody());
JsonNode jsonNode;
try
{
jsonNode = mapper.readTree(response.getBody());
token = jsonNode.get("access_token").asText();
}
catch (Exception e)
{
System.out.println(e.toString());
}
System.out.println(token);
类似的请求在RestClient、Postman或curl上运行吗?是的,我在Postman上尝试过,API运行正常类似的请求在RestClient、Postman或curl上运行吗?是的,我在Postman上尝试过,API运行正常
org.springframework.web.client.HttpClientErrorException: 417 Expectation Failed
at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:101) ~[spring-web-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:122) ~[spring-web-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:102) ~[spring-web-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:778) ~[spring-web-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736) ~[spring-web-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670) ~[spring-web-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:445) ~[spring-web-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at monitor.ScheduledTasks.getToken(ScheduledTasks.java:86) ~[classes!/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_211]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_211]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_211]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_211]
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_211]
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [na:1.8.0_211]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_211]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [na:1.8.0_211]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_211]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_211]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_211]