我之前寫過兩個接口服務user和student,現在將它完善一下
user,創建一個接口 localhost:12003/getUserList 返回一個json數據,用來做測試
UserApi.java
package com.springcloud.demo.api;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.springcloud.demo.domain.BaseJSON;
import com.springcloud.demo.domain.po.UserPO;
import com.springcloud.demo.server.UserService;
@RestController
public class UserApi {
@Autowired
public UserService userService;
@RequestMapping(value="/test",method=RequestMethod.GET)
public String test() {
return "Hello World";
}
@RequestMapping(value="/getUserList",method=RequestMethod.GET)
public BaseJSON getUserList() {
BaseJSON json = new BaseJSON();
List<UserPO> list = userService.getUserList();
if(list.size()<0||list==null) {
json.setCode(11);
json.setMessage("失敗");
}else {
json.setResult(list);
}
return json;
}
}
Service層實現
package com.springcloud.demo.server.imp;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import com.springcloud.demo.domain.po.UserPO;
import com.springcloud.demo.server.UserService;
@Service
public class UserServiceImp implements UserService{
@Override
public List<UserPO> getUserList() {
List<UserPO> list = new ArrayList<>();
UserPO user = new UserPO();
user.setId(1);
user.setUsername("lg");
user.setPassword("123456");
list.add(user);
user.setId(2);
user.setUsername("lxy");
user.setPassword("123456");
list.add(user);
return list;
}
}
Student 調用 user的restFul接口,獲取數據,然後將數據重新封裝
首先我們需要注入RestTemplate,這裏我們圖方便加載BootMain中了,使用@LoadBalanced註解標明
package com.springcloud.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Controller
@SpringBootApplication
@ApiIgnore
@EnableScheduling
@EnableDiscoveryClient
@EnableFeignClients
@EnableSwagger2
@EnableHystrixDashboard
public class BootMain {
public static void main(String[] args) {
SpringApplication.run(BootMain.class, args);
}
@RequestMapping("/help")
public String help() {
return "redirect:swagger-ui.html";
}
/**
重點看這裏
*/
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
service調用實現
【注】:
1、RestTemplate 訪問的URL不能攜帶ip和端口號,不然會報錯,改成你所調用的服務在註冊中心註冊的名字
報錯
No instances available for 192.168.xx.xx
No instances available for [IP]
2、使用Entity接收RestTemplate獲取到的值
@Autowired
private RestTemplate rt;
//ResponseEntity<BaseJSON> re = rt.getForEntity(url, BaseJSON.class);
HttpEntity<BaseJSON> re = rt.getForEntity(url, BaseJSON.class);
BaseJSON json = re.getBody();
獲取到的json就是我門的目標json對象(BaseJSON自己封裝的),裏面擁有從user獲取到的全部數據
3、在獲取對象時候,我們得到的Object無法直接轉爲目標對象StudentPO,我們可以先將其轉爲json字符串,然後再轉爲我們的目標對象,需要在pom.xml中加入jar包,具體java<——>操作可見:json數據對象操作
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
package com.springcloud.demo.service.imp;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.springcloud.demo.domain.BaseJSON;
import com.springcloud.demo.domain.po.StudentPO;
import com.springcloud.demo.domain.po.UserPO;
import com.springcloud.demo.service.StudentService;
import net.sf.json.JSONObject;
@Service
public class StudentServiceImp implements StudentService{
@Autowired
private RestTemplate rt;
@Override
public List<StudentPO> getStudentList() {
//使用RestTemplate
String url = "http://user/getUserList";
/**
* String url = "http://192.168.99.115:12001/user/getUserList";
* 報錯:No instances available for [IP]
*/
//ResponseEntity<BaseJSON> re = rt.getForEntity(url, BaseJSON.class);
HttpEntity<BaseJSON> re = rt.getForEntity(url, BaseJSON.class);
BaseJSON json = re.getBody();
List<UserPO> userList = new ArrayList();
List<StudentPO> studentList = new ArrayList();
if(json.getCode()==0) {
userList = (List<UserPO>) json.getResult();
for(Object obj:userList) {
JSONObject jsonObject = JSONObject.fromObject(obj);
UserPO user = (UserPO)jsonObject.toBean(jsonObject,UserPO.class);
StudentPO student = new StudentPO();
student.setId(user.getId());
student.setName(user.getUsername());
student.setPassword(user.getUsername());
student.setGrade(1);
studentList.add(student);
}
}else {
studentList = null;
}
return studentList;
}
}