所以我们需要写入公共模块
自己随便写入然后在pom中添加
并且在订单中调用用户服务需要使用restTemlate这个
UserController
package cn.jiedada.web.controller; import cn.jiedada.domain.User; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/provider") public class UuserController { @RequestMapping("/") public String home() { return "Hello world"; } @RequestMapping(value = "/user/{id}",method = RequestMethod.GET) public User getUserById(@PathVariable("id") Long id){ System.out.println("这是user中的getUser方法被调用"); return new User(id,"user"); } }
OrderController
package cn.jiedada.web.controller; import cn.jiedada.domain.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController @RequestMapping("/customer") public class OrderController { @Autowired private RestTemplate restTemplate; @RequestMapping("/") public String home() { return "Hello world"; } /*这个方法是我们去掉user_service_2000中的数据 所以需要使用restTemplate的类 * */ @RequestMapping("/order/{id}") public User getUserById(@PathVariable("id")Long id){ //拼接字符串 String url="http://localhost:2000/provider/user/"+id; //通过这个方法调用我们的 return restTemplate.getForObject(url,User.class); } }
RestTemplate
package cn.jiedada.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class WebConfig { @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } }
出现这个页面