在实际开发中需要调用第三方接口数据,或者是调用另外一个服务接口时,我们可以使用spring
框架提供的 RestTemplate
类可用于在应用中调用 rest 服务,它简化了与 http
服务的通信方式,统一了 restful
的标准,封装了 http
链接, 我们只需要传入 url 及返回值类型即可。
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>2.2.9.RELEASEversion>
dependency>
将RestTemplate 交给spring容器管理,方便直接使用
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
@RestController
@RequestMapping("/consumer/depart")
public class DepartController {
@Autowired
private RestTemplate restTemplate;
private static final String SERVICE_PROVIDER = "http://localhost:8081";
@PostMapping("/save")
public boolean saveHandle(@RequestBody DepartVO depart) {
String url = SERVICE_PROVIDER + "/provider/depart/save";
return restTemplate.postForObject(url, depart, Boolean.class);
}
@DeleteMapping("/del/{id}")
public void deleteHandle(@PathVariable("id") int id) {
String url = SERVICE_PROVIDER + "/provider/depart/del/" + id;
restTemplate.delete(url);
}
@PutMapping("/update")
public void updateHandle(@RequestBody DepartVO depart) {
String url = SERVICE_PROVIDER + "/provider/depart/update";
restTemplate.put(url, depart, Boolean.class);
}
@GetMapping("/get/{id}")
public DepartVO getHandle(@PathVariable("id") int id) {
String url = SERVICE_PROVIDER + "/provider/depart/get/" + id;
return restTemplate.getForObject(url, DepartVO.class);
}
@GetMapping("/list")
public List<DepartVO> listHandle() {
String url = SERVICE_PROVIDER + "/provider/depart/list/";
return restTemplate.getForObject(url, List.class);
}
}
消费者调用http服务接口
服务端接收到请求
一文吃透接口调用神器RestTemplate
RestTemplate全网最强总结(永久更新)
普通表单默认为 application/x-www-form-urlencoded 类型的请求。
入参要求:
请求类型: POST
参数名:parm
参数值为json字符串,内部参数如下
{
"requestBody": {
"condition": {
"currentPageNo": "1",
"pageSize": "1000"
}
}
}
private List<SysUnitRelation> queryUnitRelation() throws JsonProcessingException {
List<SysUnitRelation> datalist = new ArrayList<>();
Map<String, Object> param = new HashMap<>(2);
Map<String, Object> requestBody = new HashMap<>(2);
Map<String, Object> condition = new HashMap<>(4);
condition.put("currentPageNo", 1);
condition.put("pageSize", 1000);
requestBody.put("condition", condition);
param.put("requestBody", requestBody);
String body = objectMapper.writeValueAsString(param);
//①:表单信息,需要放在MultiValueMap中,MultiValueMap相当于Map>
MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<>();
//调用add方法填充表单数据(表单名称:值)
postParameters.add("parm", body);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(postParameters, headers);
QxbResponse unitRelationResult = restTemplate.postForObject(unitRelationUrl, httpEntity, QxbResponse.class);
log.info("查询中台经营组织、业务单元、产出线映射关系:" + unitRelationUrl + "结束\n返回结果:" + unitRelationResult);
String jsonResult = objectMapper.writeValueAsString(unitRelationResult.getContent());
if (unitRelationResult.isResult() && ObjectUtils.isNotEmpty(unitRelationResult.getContent())) {
datalist = objectMapper.readValue(jsonResult, new TypeReference<List<SysUnitRelation>>() {
});
}
return datalist;
}