使用restTemplate遠程調(diào)controller路徑取數(shù)據(jù)
restTemplate遠程調(diào)controller路徑取數(shù)據(jù)
Spring的RestTemplate提供了很多對HTTP method的支持,這里主要說常用的get和post。
使用環(huán)境為springboot
首先要寫相關(guān)配置類,舉例:
@Configuration
public class Config {
@Autowired
RestTemplateBuilder builder;
@Bean
public RestTemplate restTemplate() {
return builder.build();
}
}
然后調(diào)目標cotroller層,比如目標cotroller層為
@RestController
@RequestMapping("/aaa")
public class TemplateController {
@PostMapping(value = "/ppp")
public List<Students> getInfo(@RequestBody String sid) {
...
return stuService.getId(areaId);
}
}
需要用post的方法去調(diào)
@Autowired
private RestTemplate restTemplate;
public List<Student> getMsg() {
String id = "111";
HttpEntity<String> entity = buildEntity(id);
String url = "http://ip:port/aaa/ppp";
return restTemplate.postForObject(url, entity, List.class);
}
private HttpEntity<String> buildEntity(String id) {
JSONObject jo = new JSONObject();
jo.put("sid", id);
String requestJson = jo.toJSONString();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
return new HttpEntity<String>(requestJson, headers);
}
再比如目標controller層為
@RestController
@RequestMapping(value = "/aaa")
public class StudentController {
@GetMapping(value = "/ggg")
public Set<Students> queryStudent(@RequestParam(value = "code") String code,
@RequestParam(value = "objectKey") String objectKey,
@RequestParam(value = "studentId") Integer studentId) {
return sService.get(code, objectKey, kindId);
}
}
需要用get的方法去調(diào)
@Autowired
private RestTemplate restTemplate;
public Set queryStudent(String ip, int port,EventRelationTask eventRelationTask) {
Integer studentId = eventRelationTask.getStudentId();
String code = eventRelationTask.getCode();
String objectKey = eventRelationTask.getObjectKey();
String url =
"http://" + ip + ":" + port + Student.PROJECTNAME + "event/queryparentnode?code=" + code + "&objectKey=" + objectKey + "&studentId=" + studentId;
Set<Student> students = new HashSet<>();
students = restTemplate.getForObject(url, Set.class); //主要這個方法
if (students != null) {
return students;
}
return new HashSet();
}
通過Spring的RestTemplate進行跨模塊調(diào)用
Spring提供了一個RestTemplate模板工具類,對基于Http的客戶端進行了封裝,并且實現(xiàn)對象與json的序列化和反序列化。首先在項目中新建controller方法
相關(guān)代碼如圖下所示:

接著我們在另外一個項目中的啟動類的位置注冊一個RestTemplate實例
相關(guān)代碼可參考圖下所示:

然后創(chuàng)建HttpTestController使用RestTemplate中最簡單的一個功能getForEntity發(fā)起了一個get請求去調(diào)用前一個項目中服務(wù)端的數(shù)據(jù)并返回結(jié)果。

最后訪問http://localhost:8080/httpTestController/queryByname?name=張三就能看到list打印傳遞的值。需要注意的是圖1是第一個項目請求的,圖2是第二個項目通過跨服務(wù)跨項目請求得來的,它們兩者的端口號是不一樣的
運行結(jié)果如下所示:

(圖1)

(圖2)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java開發(fā)只要tomcat設(shè)計模式用的好下班就能早
這篇文章主要為大家介紹了java開發(fā)只要tomcat設(shè)計模式的示例詳解,<BR>只要設(shè)計模式用的好下班就能早,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
SpringBoot執(zhí)行異步任務(wù)Async介紹
這篇文章主要為大家介紹了SpringBoot執(zhí)行異步任務(wù)Async示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09
Sharding-jdbc報錯:Missing the data source
在使用MyBatis-plus進行數(shù)據(jù)操作時,新增Order實體屬性后,出現(xiàn)了數(shù)據(jù)源缺失的提示錯誤,原因是因為userId屬性值使用了隨機函數(shù)生成的Long值,這與sharding-jdbc的路由規(guī)則計算不匹配,導(dǎo)致無法找到正確的數(shù)據(jù)源,通過調(diào)整userId生成邏輯2024-11-11

