更多内容请浏览本人博客
在使用RestTemplate 时,直接使用
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<QzhResult> responseEntity = restTemplate.getForEntity("http://MY-SERVER/token/{1}", String.class, "aa");
发现url只能填写项目地址,而不能填注册中心id,填id会爆如下错误:
org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://{myserver:port}/Thingworx/Things/{...}/Services/findUser":
{myserver}; nested exception is java.net.UnknownHostException: {myserver}
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:607)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:475)
at spring.web.services.impl.AccountSerivceImpl.findUser(AccountSerivceImpl.java:115)
at spring.web.controller.PageController.test(PageController.java:89)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)....
解决办法如下:
添加Javabean配置
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
注入RestTemplate:
@Autowired
RestTemplate restTemplate;//定义为私有可能会报错
使用:
ResponseEntity<QzhResult> responseEntity = restTemplate.getForEntity("http://MY-SERVER/token/{1}", String.class, "aa");