使用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實現(xiàn)的連續(xù)奇數(shù)(n+2*x)是合數(shù)的算法題暴力算法
這篇文章主要介紹了Java實現(xiàn)的連續(xù)奇數(shù)(n+2*x)是合數(shù)的算法題暴力算法,本文包含運算結(jié)果和實現(xiàn)代碼,需要的朋友可以參考下2014-09-09
Mybatis-plus實現(xiàn)主鍵自增和自動注入時間的示例代碼
這篇文章主要介紹了Mybatis-plus實現(xiàn)主鍵自增和自動注入時間的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
spring?java?動態(tài)獲取consul?K/V的方法
這篇文章主要介紹了spring?java?動態(tài)獲取consul?K/V的相關(guān)資料,主要包括springConsul配置kv路徑以及自動注入consulKV到服務(wù)中,本文給大家介紹的非常詳細,需要的朋友可以參考下2023-10-10
Spring配置shiro時自定義Realm中屬性無法使用注解注入的解決辦法
今天小編就為大家分享一篇關(guān)于Spring配置shiro時自定義Realm中屬性無法使用注解注入的解決辦法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03

