diff --git a/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2JsonDecoder.java b/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2JsonDecoder.java index 2e9d30ca5b..3508487681 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2JsonDecoder.java +++ b/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2JsonDecoder.java @@ -117,13 +117,13 @@ public class Jackson2JsonDecoder extends Jackson2CodecSupport implements HttpMes return value; } catch (InvalidDefinitionException ex) { - throw new CodecException("Type definition error: " + ex.getMessage(), ex); + throw new CodecException("Type definition error: " + ex.getType(), ex); } catch (JsonProcessingException ex) { - throw new DecodingException("JSON parse error: " + ex.getMessage(), ex); + throw new DecodingException("JSON decoding error: " + ex.getOriginalMessage(), ex); } catch (IOException ex) { - throw new CodecException("I/O error while reading: " + ex.getMessage(), ex); + throw new DecodingException("I/O error while parsing input stream", ex); } }); } diff --git a/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2JsonEncoder.java b/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2JsonEncoder.java index 8f15890c39..555f7d2685 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2JsonEncoder.java +++ b/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2JsonEncoder.java @@ -32,6 +32,7 @@ import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.exc.InvalidDefinitionException; import com.fasterxml.jackson.databind.type.TypeFactory; import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; @@ -51,7 +52,6 @@ import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.util.Assert; import org.springframework.util.MimeType; - /** * Encode from an {@code Object} stream to a byte stream of JSON objects, * using Jackson 2.9. @@ -107,7 +107,7 @@ public class Jackson2JsonEncoder extends Jackson2CodecSupport implements HttpMes @Override public boolean canEncode(ResolvableType elementType, MimeType mimeType) { Class clazz = elementType.resolve(Object.class); - return Object.class.equals(clazz) || + return (Object.class == clazz) || !String.class.isAssignableFrom(elementType.resolve(clazz)) && this.objectMapper.canSerialize(clazz) && supportsMimeType(mimeType); } @@ -166,11 +166,14 @@ public class Jackson2JsonEncoder extends Jackson2CodecSupport implements HttpMes try { writer.writeValue(outputStream, value); } + catch (InvalidDefinitionException ex) { + throw new CodecException("Type definition error: " + ex.getType(), ex); + } catch (JsonProcessingException ex) { - throw new EncodingException("JSON encoding error: " + ex.getMessage(), ex); + throw new EncodingException("JSON encoding error: " + ex.getOriginalMessage(), ex); } catch (IOException ex) { - throw new CodecException("I/O error while writing: " + ex.getMessage(), ex); + throw new IllegalStateException("Unexpected I/O error while writing to data buffer", ex); } return buffer; 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 cf6de65504..03fd8253be 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -23,6 +23,7 @@ import java.util.function.Function; import javax.xml.XMLConstants; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; +import javax.xml.bind.UnmarshalException; import javax.xml.bind.Unmarshaller; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlSchema; @@ -37,6 +38,7 @@ import reactor.core.publisher.Mono; import org.springframework.core.ResolvableType; import org.springframework.core.codec.AbstractDecoder; +import org.springframework.core.codec.CodecException; import org.springframework.core.codec.DecodingException; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.util.ClassUtils; @@ -92,8 +94,7 @@ public class Jaxb2XmlDecoder extends AbstractDecoder { MimeType mimeType, Map hints) { Class outputClass = elementType.getRawClass(); - Flux xmlEventFlux = - this.xmlEventDecoder.decode(inputStream, null, mimeType, hints); + Flux xmlEventFlux = this.xmlEventDecoder.decode(inputStream, null, mimeType, hints); QName typeName = toQName(outputClass); Flux> splitEvents = split(xmlEventFlux, typeName); @@ -101,6 +102,26 @@ public class Jaxb2XmlDecoder extends AbstractDecoder { return splitEvents.map(events -> unmarshal(events, outputClass)); } + private Object unmarshal(List events, Class outputClass) { + try { + Unmarshaller unmarshaller = this.jaxbContexts.createUnmarshaller(outputClass); + XMLEventReader eventReader = StaxUtils.createXMLEventReader(events); + if (outputClass.isAnnotationPresent(XmlRootElement.class)) { + return unmarshaller.unmarshal(eventReader); + } + else { + JAXBElement jaxbElement = unmarshaller.unmarshal(eventReader, outputClass); + return jaxbElement.getValue(); + } + } + catch (UnmarshalException ex) { + throw new DecodingException("Could not unmarshal XML to " + outputClass, ex); + } + catch (JAXBException ex) { + throw new CodecException("Invalid JAXB configuration", ex); + } + } + /** * Returns the qualified name for the given class, according to the mapping rules * in the JAXB specification. @@ -120,8 +141,8 @@ public class Jaxb2XmlDecoder extends AbstractDecoder { namespaceUri = annotation.namespace(); } else { - throw new IllegalArgumentException("Outputclass [" + outputClass + "] is " + - "neither annotated with @XmlRootElement nor @XmlType"); + throw new IllegalArgumentException("Output class [" + outputClass.getName() + + "] is neither annotated with @XmlRootElement nor @XmlType"); } if (JAXB_DEFAULT_ANNOTATION_VALUE.equals(localPart)) { @@ -129,8 +150,7 @@ public class Jaxb2XmlDecoder extends AbstractDecoder { } if (JAXB_DEFAULT_ANNOTATION_VALUE.equals(namespaceUri)) { Package outputClassPackage = outputClass.getPackage(); - if (outputClassPackage != null && - outputClassPackage.isAnnotationPresent(XmlSchema.class)) { + if (outputClassPackage != null && outputClassPackage.isAnnotationPresent(XmlSchema.class)) { XmlSchema annotation = outputClassPackage.getAnnotation(XmlSchema.class); namespaceUri = annotation.namespace(); } @@ -142,21 +162,20 @@ public class Jaxb2XmlDecoder extends AbstractDecoder { } /** - * Split a flux of {@link XMLEvent}s into a flux of XMLEvent lists, one list for each - * branch of the tree that starts with the given qualified name. - * That is, given the XMLEvents shown - * {@linkplain XmlEventDecoder here}, - * and the {@code desiredName} "{@code child}", this method - * returns a flux of two lists, each of which containing the events of a particular - * branch of the tree that starts with "{@code child}". + * Split a flux of {@link XMLEvent}s into a flux of XMLEvent lists, one list + * for each branch of the tree that starts with the given qualified name. + * That is, given the XMLEvents shown {@linkplain XmlEventDecoder here}, + * and the {@code desiredName} "{@code child}", this method returns a flux + * of two lists, each of which containing the events of a particular branch + * of the tree that starts with "{@code child}". *
    - *
  1. The first list, dealing with the first branch of the tree + *
  2. The first list, dealing with the first branch of the tree: *
      *
    1. {@link javax.xml.stream.events.StartElement} {@code child}
    2. *
    3. {@link javax.xml.stream.events.Characters} {@code foo}
    4. *
    5. {@link javax.xml.stream.events.EndElement} {@code child}
    6. *
    - *
  3. The second list, dealing with the second branch of the tree + *
  4. The second list, dealing with the second branch of the tree: *
      *
    1. {@link javax.xml.stream.events.StartElement} {@code child}
    2. *
    3. {@link javax.xml.stream.events.Characters} {@code bar}
    4. @@ -166,57 +185,47 @@ public class Jaxb2XmlDecoder extends AbstractDecoder { * */ Flux> split(Flux xmlEventFlux, QName desiredName) { - return xmlEventFlux - .flatMap(new Function>>() { - - private List events = null; - - private int elementDepth = 0; - - private int barrier = Integer.MAX_VALUE; - - @Override - public Publisher> apply(XMLEvent event) { - if (event.isStartElement()) { - if (this.barrier == Integer.MAX_VALUE) { - QName startElementName = event.asStartElement().getName(); - if (desiredName.equals(startElementName)) { - this.events = new ArrayList(); - this.barrier = this.elementDepth; - } - } - this.elementDepth++; - } - if (this.elementDepth > this.barrier) { - this.events.add(event); - } - if (event.isEndElement()) { - this.elementDepth--; - if (this.elementDepth == this.barrier) { - this.barrier = Integer.MAX_VALUE; - return Mono.just(this.events); - } - } - return Mono.empty(); - } - }); + return xmlEventFlux.flatMap(new SplitFunction(desiredName)); } - private Object unmarshal(List events, Class outputClass) { - try { - Unmarshaller unmarshaller = this.jaxbContexts.createUnmarshaller(outputClass); - XMLEventReader eventReader = StaxUtils.createXMLEventReader(events); - if (outputClass.isAnnotationPresent(XmlRootElement.class)) { - return unmarshaller.unmarshal(eventReader); - } - else { - JAXBElement jaxbElement = - unmarshaller.unmarshal(eventReader, outputClass); - return jaxbElement.getValue(); - } + + private static class SplitFunction implements Function>> { + + private final QName desiredName; + + private List events; + + private int elementDepth = 0; + + private int barrier = Integer.MAX_VALUE; + + public SplitFunction(QName desiredName) { + this.desiredName = desiredName; } - catch (JAXBException ex) { - throw new DecodingException(ex.getMessage(), ex); + + @Override + public Publisher> apply(XMLEvent event) { + if (event.isStartElement()) { + if (this.barrier == Integer.MAX_VALUE) { + QName startElementName = event.asStartElement().getName(); + if (this.desiredName.equals(startElementName)) { + this.events = new ArrayList<>(); + this.barrier = this.elementDepth; + } + } + this.elementDepth++; + } + if (this.elementDepth > this.barrier) { + this.events.add(event); + } + if (event.isEndElement()) { + this.elementDepth--; + if (this.elementDepth == this.barrier) { + this.barrier = Integer.MAX_VALUE; + return Mono.just(this.events); + } + } + return Mono.empty(); } } diff --git a/spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlEncoder.java b/spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlEncoder.java index 484f7dbdd3..ef94f4501e 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlEncoder.java +++ b/spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlEncoder.java @@ -11,7 +11,7 @@ * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and - * limitations under the License. + * limitations under the License.H */ package org.springframework.http.codec.xml; @@ -20,8 +20,8 @@ import java.io.OutputStream; import java.nio.charset.StandardCharsets; import java.util.Map; import javax.xml.bind.JAXBException; +import javax.xml.bind.MarshalException; import javax.xml.bind.Marshaller; -import javax.xml.bind.UnmarshalException; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @@ -75,17 +75,16 @@ public class Jaxb2XmlEncoder extends AbstractSingleValueEncoder { DataBuffer buffer = dataBufferFactory.allocateBuffer(1024); OutputStream outputStream = buffer.asOutputStream(); Class clazz = ClassUtils.getUserClass(value); - Marshaller marshaller = jaxbContexts.createMarshaller(clazz); + Marshaller marshaller = this.jaxbContexts.createMarshaller(clazz); marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.name()); marshaller.marshal(value, outputStream); return Flux.just(buffer); } - catch (UnmarshalException ex) { - return Flux.error(new EncodingException( - "Could not unmarshal to [" + value.getClass() + "]: " + ex.getMessage(), ex)); + catch (MarshalException ex) { + return Flux.error(new EncodingException("Could not marshal " + value.getClass() + " to XML", ex)); } catch (JAXBException ex) { - return Flux.error(new CodecException("Could not instantiate JAXBContext: " + ex.getMessage(), ex)); + return Flux.error(new CodecException("Invalid JAXB configuration", ex)); } } diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java index 67cbcf634d..d731af7f5e 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java @@ -218,7 +218,7 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener return readJavaType(javaType, inputMessage); } - private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) { + private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) throws IOException { try { if (inputMessage instanceof MappingJacksonInputMessage) { Class deserializationView = ((MappingJacksonInputMessage) inputMessage).getDeserializationView(); @@ -230,13 +230,10 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener return this.objectMapper.readValue(inputMessage.getBody(), javaType); } catch (InvalidDefinitionException ex) { - throw new HttpMessageConversionException("Type definition error: " + ex.getMessage(), ex); + throw new HttpMessageConversionException("Type definition error: " + ex.getType(), ex); } catch (JsonProcessingException ex) { - throw new HttpMessageNotReadableException("JSON parse error: " + ex.getMessage(), ex); - } - catch (IOException ex) { - throw new HttpMessageNotReadableException("I/O error while reading: " + ex.getMessage(), ex); + throw new HttpMessageNotReadableException("JSON parse error: " + ex.getOriginalMessage(), ex); } } @@ -287,8 +284,11 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener generator.flush(); } + catch (InvalidDefinitionException ex) { + throw new HttpMessageConversionException("Type definition error: " + ex.getType(), ex); + } catch (JsonProcessingException ex) { - throw new HttpMessageNotWritableException("Could not write JSON document: " + ex.getMessage(), ex); + throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getOriginalMessage(), ex); } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java index d49861df48..07faf9c381 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java @@ -225,7 +225,7 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements } } catch (IOException ex) { - throw new HttpMessageNotReadableException("Could not read document: " + ex.getMessage(), ex); + throw new HttpMessageNotReadableException("I/O error while reading input message", ex); } if (body == NO_VALUE) {