//声明 private RestTemplate rsp; private final HttpHeaders headers;
private TaskRunner() {
? ? rsp = new RestTemplate(); // ?headers = new HttpHeaders(); ? ? headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8")); }
//调用代码段
{ List<Object> items; String jsonStr = JsonMapper.toJsonString(items);
String tmpUrl = "http://localhost:9001/service/base/update"; post(tmpUrl, jsonStr); }
//基础方法
private void post(String requestUrl, String json){ ? ? ActionResult result=this.postObject(requestUrl, json,new ParameterizedTypeReference<ActionResult>(){}); ? ? System.out.println(result); } private <P,R> R postObject(String url, P param, ParameterizedTypeReference<R> respType){ ? ? HttpEntity<P> p=new HttpEntity<P>(param,headers); ? ? ResponseEntity<R> resp=rsp.exchange(url, HttpMethod.POST,p,respType); ? ? return resp.getBody(); }
//被调用接口声明
@RestController @RequestMapping("/service/base") @Api(tags = "服务接口") public class CommonAdapterController { @PostMapping("/update") @ApiOperation(value="接收分发信息。",httpMethod = "POST") @ApiImplicitParam(paramType="body",dataType = "Object", required = true,name="obj",value="信息") public ActionResult<String> update(@RequestBody Object obj){ ? ?//TODO 待实现 ?return null; } }
|