Skip to content

MarshallingView does not support JAXBElement value by default [SPR-11827] #16446

@spring-projects-issues

Description

@spring-projects-issues

Stephen Medders opened SPR-11827 and commented

The method locateToBeMarshalled in org.springframework.web.servlet.view.xml.MarshallingView checks to see if the object from the model is supported by the marshaller, and throws an exception if it is not:

if (!this.marshaller.supports(obj.getClass())) {
    throw new IllegalStateException("Model object [" + obj + "] retrieved via key [" +
this.modelKey + "] is not supported by the Marshaller");
}

In some circumstances, a service may need to marshal an object that requires wrapping in a JAXBElement before marshalling. In this case, the obj variable will be of class JAXBElement. Calls to marshaller.supports(obj) when obj is a JAXBElement always returns false, at least for the Spring Jaxb2Marshaller. An additional check should be added so that if obj is a JAXBElement, then check if the marshaller supports the class of that elements value:

this.marshaller.supports(jaxbElement.getDeclaredType());

Affects: 4.0.5

Referenced from: commits e4aabd5

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions