如何实现springboot中controller之间的相互调用
springboot controller之间相互调用
SpringBoot之间内部调用
@Autowired private RestTemplate restTemplate ;//自动装配restTemplate -------------------返回json字符串类型---------------------------------- @RequestMapping("/selectHospatal") @ResponseBody public String selectHospatal() throws Exception { //返回结果 pageNum PageData result = new PageData(); PageData pageData =getPageData(); int page=Integer.parseInt(pageData.getString("page")); int limit=Integer.parseInt(pageData.getString("limit")); String name=""; name=pageData.getString("name"); User user = (User) SecurityUtils.getSubject().getPrincipal(); /* 管理员注册为0 患者为1药厂注册为3 医生注册为4 */ if(name==null){ name=""; } String forObject=""; int status=user.getStatus();//类型 String zsId=user.getType_id();//类型id if(status==5){//诊室注册为5//getManageurl是别的springboot服务的地址如:localhost:8080 String url = getManageurl+"/doctorController/selectHospatal?crid="+zsId+"&name="+name+"&page="+page+"&limit="+limit; forObject = restTemplate.getForObject(url, String.class); } if(status==6){//科室注册为6 String url = getManageurl+"/doctorController/selectHospatal?dtid="+zsId+"&name="+name+"&page="+page+"&limit="+limit; forObject = restTemplate.getForObject(url, String.class); } if(status==2){//医院注册为2 String url = getManageurl+"/doctorController/selectHospatal?houid="+zsId+"&name="+name+"&page="+page+"&limit="+limit; forObject = restTemplate.getForObject(url, String.class); } return forObject;//返回的是json字符串类型 } --------------返回PageData对象--------------------------------- /** * 查询数据 * @return * @throws Exception */ @RequestMapping("/viewd") @ResponseBody public PageData viewd() throws Exception{ //参数 PageData pageData = getPageData(); try { String url = getManageurl+"/doctorController/view?id="+pageData.getString("id"); // String forObject = restTemplate.getForObject(url, String.class); ResponseEntity<PageData> userResponseEntity = RestTemplateUtil.postJson(url, "", PageData.class, restTemplate); PageData body = userResponseEntity.getBody(); return body; } catch (Exception e) { logger.error(e.toString()); throw new Exception(e.toString()); } }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。
赞 (0)