RestTemplate实现HttpRequest请求

直接撸代码

@PostMapping("/certificate")
    public String certificate(String verSummary) throws JsonProcessingException {
        String url = "http://10.xx.xxx.xx:xxxx/SignServer/Sign";
        List list = new ArrayList();
        list.add(verSummary);

        Map req = new HashMap();
        req.put("VerSummary",list);
        String json = this.ObjectToJson(req);
        System.out.println("json is " + json);


        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type","application/json");
        HttpEntity httpEntity = new HttpEntity(json,headers);
        ResponseEntity<Map> res = this.restTemplate.exchange(url, HttpMethod.POST, httpEntity, Map.class);

        //ResponseEntity<Map> res = restTemplate.postForEntity( url, httpEntity , Map.class );
        Map cerResult = res.getBody();
        System.out.println("cerResult "+cerResult);
        String resultCode = (String)cerResult.get("ResultCode");
        return resultCode;
    }

其中,object转json的实现如下:

private final static ObjectMapper mapper = new ObjectMapper();
 
public String ObjectToJson(Object o) throws JsonProcessingException {
        ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
        String json = ow.writeValueAsString(o);
        return json;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值