尽管在Postman上工作,但在Spring中仍获得UnknownHostException

时间:2019-07-16 13:43:44

标签: java spring http resttemplate

我有一个Spring应用程序,我试图在该URL上执行一个GET请求,该URL已被确认可以通过Postman运行。

GET请求受Oauth的保护,因此我有Authorization令牌,放置在标头中。唯一的其他标头是任意的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函数上引发错误

Stacktrace:

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

1 个答案:

答案 0 :(得分:0)

由于您确定Postman可以使用相同的代码,因此我假设您所有的Constants值都正确放置了。在标头值中,您只需像这样更改Authorization语法:

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

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

Constants.BEARER="Bearer ";