gpt4 book ai didi

java - 尽管在 Postman 上工作,但在 Spring 中出现 UnknownHostException

转载 作者:太空宇宙 更新时间:2023-11-04 09:31:20 24 4
gpt4 key购买 nike

我有一个 Spring 应用程序,我正在尝试对已确认通过 Postman 进行工作的 URL 执行 GET 请求。

GET 请求受 Oauth 保护,因此我在 header 中放置了 Authorization token 。唯一的其他 header 是任意 app_id

代码中的常量如下:

Constants.AUTHORIZATION = "Authorization"
Constants.APP_ID_NAME = "app_id"
Constants.APP_ID = "123455"
Constants.APIS_URL = "https://someconfidentialurl.com/apis"
Constants.BEARER = "Bearer "

请求如下:

private HttpEntity createHttpEntity()
{
HttpHeaders headers = new HttpHeaders();
headers.set(Constants.AUTHORIZATION, Constants.BEARER + token);
headers.set(Constants.APP_ID_NAME, Constants.APP_ID);
headers.setContentType(MediaType.APPLICATION_JSON);

return new HttpEntity(headers);
}

RestTemplate restTemplate = new RestTemplate();

ResponseEntity<String> response =
restTemplate.getForEntity(Constants.APIS_URL, String.class, createHttpEntity());

它在 getForEntity 函数上抛出错误

堆栈跟踪:

org.springframework.web.client.ResourceAccessException: I/O error on GET request for "https://someurl/api/publisher/apis": someurl.com; nested exception is java.net.UnknownHostException: someurl.com
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:744) ~[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.getForEntity(RestTemplate.java:338) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at monitor.util.TasksUtil.getApis(TasksUtil.java:33) ~[classes/:na]
at monitor.ScheduledTasks.Monitor(ScheduledTasks.java:23) ~[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]
Caused by: java.net.UnknownHostException: someurl.com
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) ~[na:1.8.0_211]
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_211]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_211]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_211]
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:666) ~[na:1.8.0_211]
at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173) ~[na:1.8.0_211]
at sun.net.NetworkClient.doConnect(NetworkClient.java:180) ~[na:1.8.0_211]
at sun.net.www.http.HttpClient.openServer(HttpClient.java:463) ~[na:1.8.0_211]
at sun.net.www.http.HttpClient.openServer(HttpClient.java:558) ~[na:1.8.0_211]
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:264) ~[na:1.8.0_211]
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367) ~[na:1.8.0_211]
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191) ~[na:1.8.0_211]
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156) ~[na:1.8.0_211]
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050) ~[na:1.8.0_211]
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177) ~[na:1.8.0_211]
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:162) ~[na:1.8.0_211]
at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:735) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
... 17 common frames omitted

最佳答案

由于您确信相同的代码可以在 Postman 中正常工作,因此我假设您的所有常量值都已正确放置。在 headers 值中,您只需更改 Authorization 语法,如下所示:

headers.set(Constants.AUTHORIZATION, Constants.BEARER+" "+ token);

或者更好的是,在 Constants.BEARER 值中添加嵌入空格

Constants.BEARER="Bearer ";

关于java - 尽管在 Postman 上工作,但在 Spring 中出现 UnknownHostException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57058693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com