简体   繁体   English

如何在Spring Boot Webflux中克隆ServerResponse

[英]How to clone ServerResponse in Spring Boot Webflux

I am using Spring Boot 2.0.1.RELEASE and functional endpoint for my REST web app. 我正在为REST Web应用程序使用Spring Boot 2.0.1.RELEASE和功能终结点。 I want to add a custom header to all outgoing response, for which I have a filter. 我想向所有传出的响应添加一个自定义标头,对此我有一个过滤器。 The problem I am facing is, how to clone the ServerResponse that's created by underneath handlers. 我面临的问题是,如何克隆由下面的处理程序创建的ServerResponse ServerResponse.from builds a new ServerResponse , but only with headers and statuses. ServerResponse.from生成一个新的ServerResponse ,但仅包含标题和状态。 How can I copy the body? 我该如何复制尸体?

Most of the methods in the ServerResponse provide you with a BodyBuilder and with the bodyBuilder you can add your body using a BodyInserters object and hopefully one of its methods. ServerResponse中的大多数方法都为您提供了BodyBuilder,并且通过bodyBuilder,您可以使用BodyInserters对象以及希望的其中一种方法添加您的身体。 See below: - 见下文: -

ServerResponse.BodyBuilder bodyBuilder = ServerResponse.status(HttpStatus.OK);
bodyBuilder.body(BodyInserters.fromObject("test"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM