Add RenderingResponse.from

This commit introduces RenderingResponse.from(RenderingResponse),
allowing for easier response filtering.
This commit is contained in:
Arjen Poutsma
2017-03-22 12:30:48 +01:00
parent 54abda5e9e
commit 24f7f26fe6
2 changed files with 17 additions and 8 deletions

View File

@@ -46,6 +46,20 @@ public interface RenderingResponse extends ServerResponse {
// Builder
/**
* Create a builder with the template name, status code, headers and model of the given response.
* @param other the response to copy the values from
* @return the created builder
*/
static Builder from(RenderingResponse other) {
Assert.notNull(other, "'other' must not be null");
DefaultRenderingResponseBuilder builder = new DefaultRenderingResponseBuilder(other.name());
builder.status(other.statusCode());
builder.headers(other.headers());
builder.modelAttributes(other.model());
return builder;
}
/**
* Create a builder with the given template name.
* @param name the name of the template to render

View File

@@ -56,14 +56,9 @@ public class RenderingResponseIntegrationTests extends AbstractRouterFunctionInt
RouterFunction<RenderingResponse> normalRoute = route(GET("/normal"), handler::render);
RouterFunction<RenderingResponse> filteredRoute = route(GET("/filter"), handler::render)
.filter(ofResponseProcessor(
response -> {
Map<String, Object> model = new LinkedHashMap<>(response.model());
model.put("qux", "quux");
return RenderingResponse.create(response.name())
.modelAttributes(model)
.build();
}));
response -> RenderingResponse.from(response)
.modelAttribute("qux", "quux")
.build()));
return normalRoute.and(filteredRoute);
}