http交互总结

本文作者通过编写小demo的经历,总结了HTTP交互中的POST方法,特别是使用@requestBody完成HTTP请求的过程。旨在为以后的开发工作提供参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天上午写个小demo,发现httppost.setEntity的参数怎么也不能获取,弄了半天才通过简单的@requestBody就完成了,所以想自己多总结一下这些交互,以便后用。。

  @Override
    public String httpCallDeployment() {//发送数据
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://localhost:8766/apiGis/test");
        String string="{\"Str\":\"1\",\"Str1\":\"1\",\"Str2\":\"1\"}";
        try {
            httpPost.setHeader("Content-Type","application/json;charset=utf-8");
            StringEntity paraEntity=new StringEntity(string);
            httpPost.setEntity(paraEntity);
            HttpResponse response = httpClient.execute(httpPost);

            HttpEntity httpEntity = response.getEntity();
            InputStream inputStream = httpEntity.getContent();
            EntityUtils.consume(httpEntity);
            httpClient.close();
        } catch (Exception e) {
            logger.error("httpTest error:"+e.getMessage());
            return "HTTP error!";
        }
        return "0";
    }
    @PostMapping("/test")//通过requestBody获取json Stirng再后续解析
    public String get111(HttpServletRequest httpServletRequest, @RequestBody String sa){
        String s= httpServletRequest.getParameter("Str");
        JSONObject jsonObject= (JSONObject) JSONObject.parse(sa);
        return "1111123";
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值