MarshallingView unwraps JAXBElement value for Marshaller.supports(Class) check
Issue: SPR-11827
This commit is contained in:
@@ -20,6 +20,7 @@ import java.io.ByteArrayOutputStream;
|
||||
import java.util.Map;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.xml.bind.JAXBElement;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.springframework.oxm.Marshaller;
|
||||
@@ -55,7 +56,7 @@ public class MarshallingView extends AbstractView {
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a new {@code MarshallingView} with no {@link Marshaller} set.
|
||||
* Construct a new {@code MarshallingView} with no {@link Marshaller} set.
|
||||
* The marshaller must be set after construction by invoking {@link #setMarshaller}.
|
||||
*/
|
||||
public MarshallingView() {
|
||||
@@ -74,7 +75,7 @@ public class MarshallingView extends AbstractView {
|
||||
|
||||
|
||||
/**
|
||||
* Sets the {@link Marshaller} to be used by this view.
|
||||
* Set the {@link Marshaller} to be used by this view.
|
||||
*/
|
||||
public void setMarshaller(Marshaller marshaller) {
|
||||
this.marshaller = marshaller;
|
||||
@@ -124,23 +125,42 @@ public class MarshallingView extends AbstractView {
|
||||
*/
|
||||
protected Object locateToBeMarshalled(Map<String, Object> model) throws IllegalStateException {
|
||||
if (this.modelKey != null) {
|
||||
Object obj = model.get(this.modelKey);
|
||||
if (obj == null) {
|
||||
Object value = model.get(this.modelKey);
|
||||
if (value == null) {
|
||||
throw new IllegalStateException("Model contains no object with key [" + this.modelKey + "]");
|
||||
}
|
||||
if (!this.marshaller.supports(obj.getClass())) {
|
||||
throw new IllegalStateException("Model object [" + obj + "] retrieved via key [" +
|
||||
if (!isEligibleForMarshalling(this.modelKey, value)) {
|
||||
throw new IllegalStateException("Model object [" + value + "] retrieved via key [" +
|
||||
this.modelKey + "] is not supported by the Marshaller");
|
||||
}
|
||||
return obj;
|
||||
return value;
|
||||
}
|
||||
for (Object obj : model.values()) {
|
||||
if (obj != null && (model.size() == 1 || !(obj instanceof BindingResult)) &&
|
||||
this.marshaller.supports(obj.getClass())) {
|
||||
return obj;
|
||||
for (Map.Entry<String, Object> entry : model.entrySet()) {
|
||||
Object value = entry.getValue();
|
||||
if (value != null && (model.size() == 1 || !(value instanceof BindingResult)) &&
|
||||
isEligibleForMarshalling(entry.getKey(), value)) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the given value from the current view's model is eligible
|
||||
* for marshalling through the configured {@link Marshaller}.
|
||||
* <p>The default implementation calls {@link Marshaller#supports(Class)},
|
||||
* unwrapping a given {@link JAXBElement} first if applicable.
|
||||
* @param modelKey the value's key in the model (never {@code null})
|
||||
* @param value the value to check (never {@code null})
|
||||
* @return whether the given value is to be considered as eligible
|
||||
* @see Marshaller#supports(Class)
|
||||
*/
|
||||
protected boolean isEligibleForMarshalling(String modelKey, Object value) {
|
||||
Class<?> classToCheck = value.getClass();
|
||||
if (value instanceof JAXBElement) {
|
||||
classToCheck = ((JAXBElement) value).getDeclaredType();
|
||||
}
|
||||
return this.marshaller.supports(classToCheck);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user