From fd69c90fcb9998e4fbbd57fbb20a3a63e1494390 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Fri, 6 Jul 2018 14:24:07 +0200 Subject: [PATCH] Add ServerResponse extensions for json, xml and html Issue: SPR-17017 --- .../server/ServerResponseExtensions.kt | 23 ++++++++++++++++++- .../server/ServerResponseExtensionsTests.kt | 20 +++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensions.kt b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensions.kt index 922effd520..e2205d0f58 100644 --- a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensions.kt +++ b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensions.kt @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,3 +42,24 @@ inline fun ServerResponse.BodyBuilder.body(publisher: Publishe */ inline fun ServerResponse.BodyBuilder.bodyToServerSentEvents(publisher: Publisher): Mono = contentType(MediaType.TEXT_EVENT_STREAM).body(publisher, object : ParameterizedTypeReference() {}) + +/** + * Shortcut for setting [MediaType.APPLICATION_JSON_UTF8] `Content-Type` header. + * @author Sebastien Deleuze + * @since 5.1 + */ +fun ServerResponse.BodyBuilder.json() = contentType(MediaType.APPLICATION_JSON_UTF8) + +/** + * Shortcut for setting [MediaType.APPLICATION_XML] `Content-Type` header. + * @author Sebastien Deleuze + * @since 5.1 + */ +fun ServerResponse.BodyBuilder.xml() = contentType(MediaType.APPLICATION_XML) + +/** + * Shortcut for setting [MediaType.TEXT_HTML] `Content-Type` header. + * @author Sebastien Deleuze + * @since 5.1 + */ +fun ServerResponse.BodyBuilder.html() = contentType(MediaType.TEXT_HTML) diff --git a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensionsTests.kt b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensionsTests.kt index be5c8f2f2d..0509970edf 100644 --- a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensionsTests.kt +++ b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensionsTests.kt @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -53,5 +53,23 @@ class ServerResponseExtensionsTests { verify(bodyBuilder, times(1)).contentType(TEXT_EVENT_STREAM) } + @Test + fun `BodyBuilder#json`() { + bodyBuilder.json() + verify(bodyBuilder, times(1)).contentType(APPLICATION_JSON_UTF8) + } + + @Test + fun `BodyBuilder#xml`() { + bodyBuilder.xml() + verify(bodyBuilder, times(1)).contentType(APPLICATION_XML) + } + + @Test + fun `BodyBuilder#html`() { + bodyBuilder.html() + verify(bodyBuilder, times(1)).contentType(TEXT_HTML) + } + class Foo }