SpringCloud——接口互相调用(RestTemplate+Ribbon)

小灰灰 2022-05-10 02:14 369阅读 0赞

我之前写过两个接口服务user和student,现在将它完善一下

user,创建一个接口 localhost:12003/getUserList 返回一个json数据,用来做测试

70

UserApi.java

  1. package com.springcloud.demo.api;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import com.springcloud.demo.domain.BaseJSON;
  8. import com.springcloud.demo.domain.po.UserPO;
  9. import com.springcloud.demo.server.UserService;
  10. @RestController
  11. public class UserApi {
  12. @Autowired
  13. public UserService userService;
  14. @RequestMapping(value="/test",method=RequestMethod.GET)
  15. public String test() {
  16. return "Hello World";
  17. }
  18. @RequestMapping(value="/getUserList",method=RequestMethod.GET)
  19. public BaseJSON getUserList() {
  20. BaseJSON json = new BaseJSON();
  21. List<UserPO> list = userService.getUserList();
  22. if(list.size()<0||list==null) {
  23. json.setCode(11);
  24. json.setMessage("失败");
  25. }else {
  26. json.setResult(list);
  27. }
  28. return json;
  29. }
  30. }

Service层实现

  1. package com.springcloud.demo.server.imp;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.springframework.stereotype.Service;
  5. import com.springcloud.demo.domain.po.UserPO;
  6. import com.springcloud.demo.server.UserService;
  7. @Service
  8. public class UserServiceImp implements UserService{
  9. @Override
  10. public List<UserPO> getUserList() {
  11. List<UserPO> list = new ArrayList<>();
  12. UserPO user = new UserPO();
  13. user.setId(1);
  14. user.setUsername("lg");
  15. user.setPassword("123456");
  16. list.add(user);
  17. user.setId(2);
  18. user.setUsername("lxy");
  19. user.setPassword("123456");
  20. list.add(user);
  21. return list;
  22. }
  23. }

Student 调用 user的restFul接口,获取数据,然后将数据重新封装

70 1

首先我们需要注入RestTemplate,这里我们图方便加载BootMain中了,使用@LoadBalanced注解标明

  1. package com.springcloud.demo;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  6. import org.springframework.cloud.netflix.feign.EnableFeignClients;
  7. import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.scheduling.annotation.EnableScheduling;
  10. import org.springframework.stereotype.Controller;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.client.RestTemplate;
  13. import springfox.documentation.annotations.ApiIgnore;
  14. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  15. @Controller
  16. @SpringBootApplication
  17. @ApiIgnore
  18. @EnableScheduling
  19. @EnableDiscoveryClient
  20. @EnableFeignClients
  21. @EnableSwagger2
  22. @EnableHystrixDashboard
  23. public class BootMain {
  24. public static void main(String[] args) {
  25. SpringApplication.run(BootMain.class, args);
  26. }
  27. @RequestMapping("/help")
  28. public String help() {
  29. return "redirect:swagger-ui.html";
  30. }
  31. /**
  32. 重点看这里
  33. */
  34. @Bean
  35. @LoadBalanced
  36. RestTemplate restTemplate() {
  37. return new RestTemplate();
  38. }
  39. }

service调用实现

【注】:

1、RestTemplate 访问的URL不能携带ip和端口号,不然会报错,改成你所调用的服务在注册中心注册的名字

  1. 报错
  2. No instances available for 192.168.xx.xx
  3. No instances available for [IP]

2、使用Entity接收RestTemplate获取到的值

  1. @Autowired
  2. private RestTemplate rt;
  3. //ResponseEntity<BaseJSON> re = rt.getForEntity(url, BaseJSON.class);
  4. HttpEntity<BaseJSON> re = rt.getForEntity(url, BaseJSON.class);
  5. BaseJSON json = re.getBody();

获取到的json就是我门的目标json对象(BaseJSON自己封装的),里面拥有从user获取到的全部数据
3、在获取对象时候,我们得到的Object无法直接转为目标对象StudentPO,我们可以先将其转为json字符串,然后再转为我们的目标对象,需要在pom.xml中加入jar包,具体java<——>操作可见:json数据对象操作

  1. <dependency>
  2. <groupId>net.sf.json-lib</groupId>
  3. <artifactId>json-lib</artifactId>
  4. <version>2.4</version>
  5. <classifier>jdk15</classifier>
  6. </dependency>
  7. package com.springcloud.demo.service.imp;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.http.HttpEntity;
  12. import org.springframework.http.ResponseEntity;
  13. import org.springframework.stereotype.Service;
  14. import org.springframework.web.client.RestTemplate;
  15. import com.springcloud.demo.domain.BaseJSON;
  16. import com.springcloud.demo.domain.po.StudentPO;
  17. import com.springcloud.demo.domain.po.UserPO;
  18. import com.springcloud.demo.service.StudentService;
  19. import net.sf.json.JSONObject;
  20. @Service
  21. public class StudentServiceImp implements StudentService{
  22. @Autowired
  23. private RestTemplate rt;
  24. @Override
  25. public List<StudentPO> getStudentList() {
  26. //使用RestTemplate
  27. String url = "http://user/getUserList";
  28. /**
  29. * String url = "http://192.168.99.115:12001/user/getUserList";
  30. * 报错:No instances available for [IP]
  31. */
  32. //ResponseEntity<BaseJSON> re = rt.getForEntity(url, BaseJSON.class);
  33. HttpEntity<BaseJSON> re = rt.getForEntity(url, BaseJSON.class);
  34. BaseJSON json = re.getBody();
  35. List<UserPO> userList = new ArrayList();
  36. List<StudentPO> studentList = new ArrayList();
  37. if(json.getCode()==0) {
  38. userList = (List<UserPO>) json.getResult();
  39. for(Object obj:userList) {
  40. JSONObject jsonObject = JSONObject.fromObject(obj);
  41. UserPO user = (UserPO)jsonObject.toBean(jsonObject,UserPO.class);
  42. StudentPO student = new StudentPO();
  43. student.setId(user.getId());
  44. student.setName(user.getUsername());
  45. student.setPassword(user.getUsername());
  46. student.setGrade(1);
  47. studentList.add(student);
  48. }
  49. }else {
  50. studentList = null;
  51. }
  52. return studentList;
  53. }
  54. }

发表评论

表情:
评论列表 (有 0 条评论,369人围观)

还没有评论,来说两句吧...

相关阅读