如何实现springboot中controller之间的相互调用

springboot controller之间相互调用 如何实现springboot中controller之间的相互调用
文章图片

SpringBoot之间内部调用

@Autowiredprivate RestTemplate restTemplate ; //自动装配restTemplate -------------------返回json字符串类型----------------------------------@RequestMapping("/selectHospatal") @ResponseBody public String selectHospatal()throws Exception {//返回结果pageNumPageData result = new PageData(); PageData pageData =https://www.it610.com/article/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(); //类型idif(status==5){//诊室注册为5//getManageurl是别的springboot服务的地址如:localhost:8080String url = getManageurl+"/doctorController/selectHospatal?crid="+zsId+"&name="+name+"&page="+page+"&limit="+limit; forObject = restTemplate.getForObject(url, String.class); }if(status==6){//科室注册为6String url = getManageurl+"/doctorController/selectHospatal?dtid="+zsId+"&name="+name+"&page="+page+"&limit="+limit; forObject = restTemplate.getForObject(url, String.class); }if(status==2){//医院注册为2String 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 = https://www.it610.com/article/getPageData(); try {String url = getManageurl+"/doctorController/view?id="+pageData.getString("id"); // String forObject = restTemplate.getForObject(url, String.class); ResponseEntity 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()); } }

【如何实现springboot中controller之间的相互调用】以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

    推荐阅读