惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Last Watchdog
The Last Watchdog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Secure Thoughts
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
T
Tor Project blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Vercel News
Vercel News
Last Week in AI
Last Week in AI
月光博客
月光博客
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
博客园 - 叶小钗
NISL@THU
NISL@THU
C
Check Point Blog
K
Kaspersky official blog
N
News and Events Feed by Topic
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
Arctic Wolf
T
Threatpost
GbyAI
GbyAI
L
LINUX DO - 热门话题
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
N
News and Events Feed by Topic
Scott Helme
Scott Helme
P
Privacy International News Feed
The Register - Security
The Register - Security
G
GRAHAM CLULEY
Recorded Future
Recorded Future
Apple Machine Learning Research
Apple Machine Learning Research
C
Cybersecurity and Infrastructure Security Agency CISA
B
Blog
Project Zero
Project Zero
Cyberwarzone
Cyberwarzone
Webroot Blog
Webroot Blog
Microsoft Security Blog
Microsoft Security Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
D
DataBreaches.Net
J
Java Code Geeks
AWS News Blog
AWS News Blog
Help Net Security
Help Net Security
Engineering at Meta
Engineering at Meta
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
Google DeepMind News
Google DeepMind News

博客园 - 华安

Unity 中区别,public 和 [SerializeField] Unity中 onCollisionEnter2D与OnTriggerEnter2D 区别 asp.netCore中给动态请求路径加客户端缓存 netCore中获取客户端真实的IP引起的思考 Unity 相机:正交 (Orthographic) vs 透视 (Perspective) unity中的button中的onclick控制面板中四个参数的意思分别是什么 微信小程序开发中触发 onshow的几种特殊情况 SQL Server备份心得 MYSQL 备份数据库 微信与支付支付功能开发 微信小程序中页面配置下拉刷新 unity中 相机没有视锥效果线框了,如何打开 JSONPath表达式 C# 中的操作JSON类 JObject cookie中的 HttpOnly 、Secure、SameSite 解释 Spring-boot 中基于 IP 的限流和自动封禁 Filter 登录 用 HMAC-SHA256 实现 TwoFA(二重验证)的坑 利用Spring Boot的 filter 结合ConcurrentHashMap 实现“同一IP每分钟最多允许300个404请求,超出后禁用30分钟访问” pixi-filters中的BackdropBlurFilter使用注意事项 PixiJS中的 SplitBitmapText.chars中的每个 BitmapText的x值分析 legend隐藏不想显示的图例 spring-boot HttpServletResponse response.sendRedirect是会跳转到 http而不是https springboot获取post请求参数 Javascript如何判断是触摸屏还是PC端 JavaScript获取鼠标点一个元素,获取鼠标点击的元素内的位置 spring-boot中配置Mongodbd的问题小结 CSS实现修改CheckBox样式 SpringBoot+Thyemleaf报错:Error resolving template Template might not exist or might not be accessible div display flex 如何出现横向滚动条
Rest Template中添加 PATCH请求。
华安 · 2025-11-25 · via 博客园 - 华安

今天在用RestTemplate做 PATCH请求的时候,提示不支持PATCH请求。查了下资料 默认的RestTemplate不支持PATCH方法‌,需要通过以下方式解决:

1. 添加必要的依赖

<dependency>
    <groupId>org.apache.httpcomponents.client5</groupId>
    <artifactId>httpclient5</artifactId>

    <version>5.5.1</version>

</dependency>

2. 配置支持PATCH的RestTemplate

package com.idmt.difyweb.config;

/*import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;*/
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;

//使用案例 https://cloud.tencent.com/developer/article/1606785

@Configuration
public class RestTemplateConfig {
   /* @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        return new RestTemplate(factory);
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        //HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setReadTimeout(5000);  // 读取超时时间
        factory.setConnectTimeout(15000); // 连接超时时间
        return factory;
    }*/

    @Bean
    public RestTemplate restTemplate() {
        // 使用HttpComponentsClientHttpRequestFactory支持PATCH
        var requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setConnectTimeout(Duration.ofSeconds(5));
        requestFactory.setReadTimeout(Duration.ofSeconds(10));

        return new RestTemplate(requestFactory);
    }
}

3. 发送PATCH请求的完整示例

@ResponseBody
    @GetMapping("/documentsstatusupdate")
    public String DocumentStatusUpdate(@RequestParam("document_id") String document_id,
                                       @RequestParam("document_statu") String document_statu
                                       ) {// 创建请求体
        String requestBody = "{\"document_ids\":[\""+document_id+"\"]}";

        String url = domainName + "datasets/" + difyDataId + "/documents/status/"+document_statu;
        ResponseEntity<String> response = restTemplate.exchange(
                url,
                HttpMethod.PATCH,
                getEntityJson(requestBody),
                String.class
        );
        return response.getBody();
    }
 private HttpEntity<String> getEntityJson(String postBody) {
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer " + accessToken);
        headers.set("Content-Type", "application/json");
        HttpEntity<String> entity = new HttpEntity<String>(postBody, headers);
        return entity;
    }

 注意:

org.apache.httpcomponents.client5org.apache.httpcomponents是Apache HttpClient库的两个不同版本系列,主要区别在于代际版本、包结构、API设计和适用场景。

代际版本与包结构

  • ‌org.apache.httpcomponents‌:对应HttpClient 4.x系列(最新4.5.13),包名为org.apache.http
  • ‌org.apache.httpcomponents.client5‌:对应HttpClient 5.x系列(如5.2.1),包名升级为org.apache.hc.client5.http,避免与旧版冲突。 ‌12

API设计改进

  • ‌4.x痛点‌:
    • 同步IO模型为主,异步支持弱(需额外依赖HttpAsyncClient)。
    • 连接管理需手动处理HttpClientConnectionManager。 ‌1
  • ‌5.x优化‌:
    • 统一同步/异步API(通过HttpClients.custom().build()自动选择)。
    • 默认启用高效连接池,支持链式调用(Fluent API)简化请求构建。 ‌13

协议与兼容性

  • ‌4.x适用场景‌:传统同步业务、Android低版本(API < 24)兼容、简单HTTP请求。
  • ‌5.x优势‌:支持HTTP/2、Reactive编程,需最低Android API 24(7.0+)。 ‌14

迁移建议

  • 新项目优先选择5.x,利用异步和HTTP/2特性。
  • 旧系统升级需替换包路径(org.apache.httporg.apache.hc.client5.http),并重构阻塞调用为异步模式