springcloud-rest

springcloud-rest

ConfigBean.java

@Configuration
public class ConfigBean { // @Configuration 相当于 spring applicationContext.xml
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

DeptConsumerController.java

@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);
    }
}
posted @ 2022-08-31 21:59  bitterteaer  阅读(14)  评论(0编辑  收藏  举报