首页 > 其他分享 >Spring Boot调用api patch接口

Spring Boot调用api patch接口

时间:2022-08-20 16:26:25浏览次数:102  
标签:body httpPatch log Spring Boot patch response String

Spring Boot调用api patch接口

示例:

  /**
     * 调用api patch接口
     *
     * @param url
     * @param jsonObject
     * @return java.lang.String
     * @author li.zhm
     * @date 2022/8/20 14:17
     * @history <author>  <time>  <version>  <desc>
     */
    public static String sendPatch(String url, JSONObject jsonObject) {
        log.info("请求patch地址:{}", url);
        String body = "";
        // 创建httpclient对象
        CloseableHttpClient client = HttpClients.createDefault();

        CloseableHttpResponse response = null;
        // 创建patch方式请求对象
        HttpPatch httpPatch = new HttpPatch(url);
        RequestConfig requestConfig =
                RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
        httpPatch.setConfig(requestConfig);
        // 装填参数
        StringEntity s = new StringEntity(jsonObject.toString(), "utf-8");
        s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        // 设置参数到请求对象中
        httpPatch.setEntity(s);
        log.info("请求参数:{}", jsonObject.toString());

        // 设置header信息, 指定报文头【Content-type】 【User-Agent】
        httpPatch.setHeader("Content-type", "application/json");
        httpPatch.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
        try {
            // 执行请求操作,并拿到结果(同步阻塞)
            response = client.execute(httpPatch);
            log.info("response 返回状态码:{}", response.getStatusLine().getStatusCode());
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK || response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
                // 获取结果实体
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    // 按指定编码转换结果实体为String类型
                    body = EntityUtils.toString(entity, "utf-8");
                    log.info("响应body:", body);
                }
                // 关闭流
                EntityUtils.consume(entity);
            } else {
                log.error("response 返回状态码异常:{}", response.getStatusLine().getStatusCode());
            }
            // 释放链接
            response.close();
        } catch (IOException e) {
            log.error("调用amf-update sub api异常,{0}", e);
            e.printStackTrace();
        } finally {
            if (client != null) {
                try {
                    client.close();
                } catch (IOException e) {
                    log.error("关闭http client异常,{0}", e);
                    e.printStackTrace();
                }
            }
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    log.error("关闭http response异常,{0}", e);
                    e.printStackTrace();
                }
            }
        }
        return body;
    }

 

标签:body,httpPatch,log,Spring,Boot,patch,response,String
From: https://www.cnblogs.com/lizm166/p/16607947.html

相关文章

  • Spring Boot调用api delete接口
     SpringBoot调用apidelete接口示例:/***调用apidelete接口**@paramurl*@authorli.zhm*@date2022/8/2014:07*@hist......
  • spring源码学习笔记1——解析xml生成BeanDefinition的过程解析
    spring源码学习笔记1——解析xml生成BeanDefinition的过程解析一丶Spring解析Xml生成BeanDefinition的流程1.指定xml路径解析xml首先需要知道xml的位置,如下我们构造了Ap......
  • 【Spring5学习笔记(4)】事务管理:
    事务1、什么是事务(1)事务是数据库操作的最基本单元,是逻辑上的一组操作,要么都成功,如果有一个失败则所有操作都失败(2)经典场景:银行转账2、事务的四个特性(ACID)(1)原子性:一组逻辑操......
  • SpringBoot 搭建和使用图形化监控界面
    我们开发好的SpringBoot服务发布到公网上,肯定希望能够对其状态和资源消耗情况进行监控,特别是对每个接口访问情况的统计,以便在发生问题时能够快速排查和分析并解决问题。......
  • ufw开机不启动,ufw inactive after reboot
    废话:最近部署debian服务器,我用ufw把ssh端口打开了,重启服务器后,ssh又连接不上了。我心想ufw开机不自启动的吗?那我服务器每次重启后我都得把显示器接上手动打开22端口,这不是......
  • 聊聊@SpringBootApplication注解
    @SpringBootApplication其实就是以下三个注解的总和@Configuration: 用于定义一个配置类@EnableAutoConfiguration :SpringBoot会自动根据你jar包的依赖来自动配置项......
  • Spring源码-xml解析
    Spring使用SAX解析xml。SAX的全称是SimpleAPIsforXML,也即XML简单应用程序接口。与DOM不同,SAX提供的访问模式是一种顺序模式,这是一种快速读写XML数据的方式。当使用SAX分......
  • Spring Boot调用api post接口
    SpringBoot调用apipost接口示例publicstaticStringsendPost(Stringurl,JSONObjectjsonObject){log.info("请求地址:{}",url);Stringbody......
  • Spring 03: 基于xml的构造方法注入
    注入方式具体有3种注入方式:通过构造方法的a.参数名称注入b.参数下标注入c.默认参数顺序注入参数名称注入School实体类packagecom.example.pojo03;public......
  • 基于SpringBoot的SSMP整合
    前言实体类开发————使用Lombok快速制作实体类Mapper开发————整合MyBatisPlus,制作数据层测试Service开发————基于MyBatisPlus进行增量开发,制作业务层测试......