From af0cb5374271ddeaab09541eb155cba4a8204d61 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Wed, 2 May 2018 11:57:40 +0200 Subject: [PATCH] Support decoding Mono in Jaxb2XmlDecoder Issue: SPR-16759 --- .../springframework/http/codec/xml/Jaxb2XmlDecoder.java | 6 ++++++ .../http/codec/xml/Jaxb2XmlDecoderTests.java | 5 +++-- .../RequestMappingMessageConversionIntegrationTests.java | 7 +++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlDecoder.java b/spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlDecoder.java index a51802eb40..18f3e6605b 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlDecoder.java +++ b/spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlDecoder.java @@ -107,6 +107,12 @@ public class Jaxb2XmlDecoder extends AbstractDecoder { return splitEvents.map(events -> unmarshal(events, outputClass)); } + @Override + public Mono decodeToMono(Publisher inputStream, ResolvableType elementType, + @Nullable MimeType mimeType, @Nullable Map hints) { + return decode(inputStream, elementType, mimeType, hints).singleOrEmpty(); + } + private Object unmarshal(List events, Class outputClass) { try { Unmarshaller unmarshaller = this.jaxbContexts.createUnmarshaller(outputClass); diff --git a/spring-web/src/test/java/org/springframework/http/codec/xml/Jaxb2XmlDecoderTests.java b/spring-web/src/test/java/org/springframework/http/codec/xml/Jaxb2XmlDecoderTests.java index b7a7954a48..b4f19fb603 100644 --- a/spring-web/src/test/java/org/springframework/http/codec/xml/Jaxb2XmlDecoderTests.java +++ b/spring-web/src/test/java/org/springframework/http/codec/xml/Jaxb2XmlDecoderTests.java @@ -23,6 +23,7 @@ import javax.xml.stream.events.XMLEvent; import org.junit.Test; import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; import reactor.test.StepVerifier; import org.springframework.core.ResolvableType; @@ -159,7 +160,7 @@ public class Jaxb2XmlDecoderTests extends AbstractDataBufferAllocatingTestCase { @Test public void decodeSingleXmlRootElement() throws Exception { Flux source = Flux.just(stringBuffer(POJO_ROOT)); - Flux output = this.decoder.decode(source, ResolvableType.forClass(Pojo.class), + Mono output = this.decoder.decodeToMono(source, ResolvableType.forClass(Pojo.class), null, Collections.emptyMap()); StepVerifier.create(output) @@ -171,7 +172,7 @@ public class Jaxb2XmlDecoderTests extends AbstractDataBufferAllocatingTestCase { @Test public void decodeSingleXmlTypeElement() throws Exception { Flux source = Flux.just(stringBuffer(POJO_ROOT)); - Flux output = this.decoder.decode(source, ResolvableType.forClass(TypePojo.class), + Mono output = this.decoder.decodeToMono(source, ResolvableType.forClass(TypePojo.class), null, Collections.emptyMap()); StepVerifier.create(output) diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingMessageConversionIntegrationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingMessageConversionIntegrationTests.java index 4ebbb811d4..c825ce6a80 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingMessageConversionIntegrationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingMessageConversionIntegrationTests.java @@ -227,6 +227,13 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq JSON, Person.class).getBody()); } + @Test // SPR-16759 + public void personTransformWithMonoAndXml() throws Exception { + assertEquals(new Person("ROBERT"), + performPost("/person-transform/mono", MediaType.APPLICATION_XML, new Person("Robert"), + MediaType.APPLICATION_XML, Person.class).getBody()); + } + @Test public void personTransformWithSingle() throws Exception { assertEquals(new Person("ROBERT"),