我一直在寻找Webclient
中补丁方法的一个例子。我的Model类中有四个字段,我需要更新一个字段(即状态字段),因此我决定使用Patch方法。但我在互联网上没有任何例子。
我在RestTemplate
中有一段代码,这里我需要在Webclient
中使用它,因为我正在迁移到Webclient
。如何实现下面的代码?
public void updateProfile(UpdateProfile profile, String uniqueId) {
HttpHeaders headers = new HttpHeaders();
MediaType mediaType = new MediaType("application", "merge-patch+json");
headers.setContentType(mediaType);
HttpEntity<UpdateProfile> entity = new HttpEntity<>(profile, headers);
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
RestTemplate restTemplate = new RestTemplate(requestFactory);
restTemplate.exchange(firebaseUrl+"/"+path+"/" + uniqueId + ".json",
HttpMethod.PATCH, entity, Void.class);
}
发布于 2020-11-26 02:49:38
你可以从web客户端使用下面的补丁方法示例,我还没有测试过,但我希望你可以使用类似的方法。
WebClient webClient = WebClient.create(firebaseUrl);
webClient.patch()
.uri("+path+" + uniqueId + ".json")
.contentType(MediaType.valueOf("application/json-patch+json"))
.bodyValue(data)
.exchange();
请记住在您的pom.xml中添加以下工件,
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
https://stackoverflow.com/questions/65009081
复制相似问题