首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Webclient中使用Patch方法?

如何在Webclient中使用Patch方法?
EN

Stack Overflow用户
提问于 2020-11-26 00:43:17
回答 1查看 790关注 0票数 1

我一直在寻找Webclient中补丁方法的一个例子。我的Model类中有四个字段,我需要更新一个字段(即状态字段),因此我决定使用Patch方法。但我在互联网上没有任何例子。

我在RestTemplate中有一段代码,这里我需要在Webclient中使用它,因为我正在迁移到Webclient。如何实现下面的代码?

代码语言:javascript
运行
复制
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);
        
    }
EN

回答 1

Stack Overflow用户

发布于 2020-11-26 02:49:38

你可以从web客户端使用下面的补丁方法示例,我还没有测试过,但我希望你可以使用类似的方法。

代码语言:javascript
运行
复制
        WebClient webClient = WebClient.create(firebaseUrl);
        webClient.patch()
                 .uri("+path+" + uniqueId + ".json")
                 .contentType(MediaType.valueOf("application/json-patch+json"))
                 .bodyValue(data)
                 .exchange();

请记住在您的pom.xml中添加以下工件,

代码语言:javascript
运行
复制
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65009081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档