Ibm cloud Bluemix环境中调用Weather rest api的问题
在Bluemix中调用气象公司RESTAPI服务时出错。在本地环境中使用相同的代码,我可以从这个调用中获得响应。请分享你的建议 代码:Ibm cloud Bluemix环境中调用Weather rest api的问题,ibm-cloud,Ibm Cloud,在Bluemix中调用气象公司RESTAPI服务时出错。在本地环境中使用相同的代码,我可以从这个调用中获得响应。请分享你的建议 代码: RestTemplate RestTemplate=new RestTemplate(); HttpHeaders=新的HttpHeaders(); headers.set(“接受”、“应用程序/json”); HttpEntity=新的HttpEntity(标题); ResponseEntity jsonString=restTemplate.exchange
RestTemplate RestTemplate=new RestTemplate();
HttpHeaders=新的HttpHeaders();
headers.set(“接受”、“应用程序/json”);
HttpEntity=新的HttpEntity(标题);
ResponseEntity jsonString=restTemplate.exchange(srcURL、HttpMethod.GET、entity、WeatherStatusWrapper.class);
错误:
2016年12月27日10:06:43 AM ERR App[ERROR]SRVE0777E:应用程序类“org.springframework.web.servlet.FrameworkServlet.processRequest:982”引发异常
2016年12月27日10:06:43 AM ERR App org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常为org.springframework.web.client.HttpClientErrorException:401未经授权
2016年12月27日10:06:43 AM ERR应用程序位于org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
2016年12月27日10:06:43 AM ERR应用程序位于org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
2016年12月27日上午10:06:43 javax.servlet.http.HttpServlet.service上的ERR应用程序(HttpServlet.java:707)
2016年12月27日10:06:43 AM ERR应用程序位于org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
2016年12月27日10:06:43 AM ERR应用程序位于com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1290)
2016年12月27日上午10:06:43[内部课程]上的ERR应用程序
2016年12月27日10:06:43 AM ERR应用程序位于org.springframework.web.client.RestTemplate.HandlerResponse(RestTemplate.java:641)
2016年12月27日10:06:43 AM ERR应用程序位于org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597)
2016年12月27日10:06:43 AM ERR应用程序位于org.springframework.web.client.restemplate.execute(restemplate.java:557)
2016年12月27日10:06:43 AM ERR应用程序位于org.springframework.web.client.restemplate.getForEntity(restemplate.java:289)
2016年12月27日10:06:43上午错误应用程序:com.techm.bluemix.smartrairport.serviceinpl.weatherserviceinpl.trackWeatherForecast(weatherserviceinpl.java:94)
2016年12月27日10:06:43 AM ERR应用程序位于com.techm.bluemix.smartrairport.Controller.WeatherController.trackWeatherForecast(WeatherController.java:47)
2016年12月27日上午10:06:43 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)上的ERR应用程序
2016年12月27日上午10:06:43 sun.reflect.NativeMethodAccessorImpl.invoke上的错误应用程序(未知源)
2016年12月27日上午10:06:43 sun.reflect.DelegatingMethodAccessorImpl.invoke上的错误应用程序(未知源)
2016年12月27日10:06:43 AM ERR应用程序位于java.lang.reflect.Method.invoke(未知源)
2016年12月27日10:06:43 AM ERR应用程序位于org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
2016年12月27日10:06:43 AM ERR应用程序,位于org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
2016年12月27日10:06:43 AM ERR应用程序位于org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
2016年12月27日10:06:43 AM ERR应用程序位于org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
2016年12月27日10:06:43 AM ERR应用程序位于org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
2016年12月27日10:06:43 AM ERR应用程序位于org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
2016年12月27日10:06:43 AM ERR应用程序位于org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
它抛出了一个未经授权的错误。请求需要身份验证。用户名和密码可以从Bluemix应用程序仪表板中的“连接”选项卡->查看凭据中获得。已通过主机的凭据您可以手动设置授权吗?DefaultHttpClient httpClient=新的DefaultHttpClient();BasicCredentialsProvider credentialsProvider=新的BasicCredentialsProvider();setCredentials(AuthScope.ANY,新用户名密码Credentials(用户,密码));httpClient.setCredentialsProvider(credentialsProvider);
RestTemplate restTemplate=new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/json");
HttpEntity entity = new HttpEntity(headers);
ResponseEntity<WeatherStatusWrapper> jsonString = restTemplate.exchange(srcURL, HttpMethod.GET, entity, WeatherStatusWrapper.class);