Background
- Upgrading from Spring Framework 4.3.30.RELEASE to 5.0.20.RELEASE
- Using oxm:jaxb2-marshaller
Problem
After upgrading the Spring Framework dependency version, the application fails to start because of:
Offending resource: class path resource [spring/application-context.xml]; nested exception is org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 77 in XML document from class path resource [spring/services/adapters.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 77; columnNumber: 90; cvc-complex-type.3.2.2: Attribute 'contextPath' is not allowed to appear in element 'oxm:jaxb2-marshaller'.
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:72) ~[spring-beans-5.0.20.RELEASE.jar:5.0.20.RELEASE]
...
Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 77 in XML document from class path resource [spring/services/adapters.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 77; columnNumber: 90; cvc-complex-type.3.2.2: Attribute 'contextPath' is not allowed to appear in element 'oxm:jaxb2-marshaller'.
...
Caused by: org.xml.sax.SAXParseException: cvc-complex-type.3.2.2: Attribute 'contextPath' is not allowed to appear in element 'oxm:jaxb2-marshaller'.
Code
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.2.xsd">
...
<!-- Line 77 is: -->
<oxm:jaxb2-marshaller id="someMarshaller" contextPath="com.marshallable.jaxb" />
Looks like starting with Spring Framework 5, internally, Spring wants something that’s compatible with spring-oxm-4.0.xsd where contextPath
changed to context-path
.
The fix was to update the schemaLocation
to use spring-beans-4.0.xsd and spring-oxm-4.0.xsd and also change the contextPath
property to context-path
like so:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.0.xsd">
...
<oxm:jaxb2-marshaller id="someMarshaller" context-path="com.marshallable.jaxb" />