patchForObject 怎么样?

patchForObject how to?

我想通过 Spring RestTemplate 发送 HTTP PATCH 请求,其中包含由以下对象表示的操作数组:

@Data
@AllArgsConstructor
public class JsonOperation {
    private String op;
    private String path;
    private String value;
}

考虑以下代码:

List<JsonOperation> operations = new ArrayList<>();
operations.add(new JsonOperation("replace", "/path1", "value1"));
operations.add(new JsonOperation("replace", "/path2", "value2"));
operations.add(new JsonOperation("replace", "/path3", "value3"));

是否可以如下使用 restTemplate 进行请求?

restTemplate.patchForObject(url+"/toPatch/"+toPatchId, operations, ResponseEntity.class);

由于一个错误,Spring 的默认 RestTemplate 不发送 Http 补丁请求。

由于 org.springframework.http.client.HttpComponentsClientHttpRequestFactory 可以用来覆盖 RestTemplate:

@Bean
public RestTemplate httpComponentsClientRestTemplate() {
    final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    requestFactory.setConnectTimeout(3000);
    requestFactory.setReadTimeout(3000));
    return new RestTemplate(requestFactory);
}