springcloud-rest

ConfigBean.java

1
2
3
4
5
6
7
@Configuration
public class ConfigBean { // @Configuration 相当于 spring applicationContext.xml
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}

DeptConsumerController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@RestController
public class DeptConsumerController {
// 理解:消费者,不应该有service层
// RestTemplate ... 供我们直接调用就可以了! 注册到spring中
// (url,实体:Map, Class<T>, responseType)
@Autowired
private RestTemplate restTemplate;

private static final String REST_URL_PREFIX = "http://localhost:8001/";
@RequestMapping("/comsumer/dept/get")
public Dept get(@PathVariable("id") Long id){
return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get?id="+id, Dept.class);
}

@RequestMapping("/comsumer/dept/list")
public List<Dept> queryAll(){
return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list", List.class);
}
}