Search code examples
springjaxb2

Using Jaxb2Marshaller to unmarshall an xml and validate against a schema


I am implementing a REST service with xml as the payload and have the following configuration to use Jaxb2Marshaller to unmarshall my xml. Here is the configuration from my spring context file

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="classesToBeBound">
            <list>
                <value>com.my.examples.Product</value>
            </list>
        </property>
        <property name="schema" value="classpath:schemadefinitions/product.xsd" />
    </bean>

On my bean Product I have just this annotation as

@XmlRootElement(name="product") public class ProductInfo {

The issue is when I make the REST request it unmarshalls xml to bean properly but doesn't perform any validation against the schema configured with the marshaller. Am I missing anything ?


Solution

  • I had to attach a validationeventhandler to the marshaller as jaxb2Mashaller.setValidationEventHandler(...) Once this is set the unmarshaller started to validate input xml.