Announcement Announcement Module
Collapse
No announcement yet.
Custom error page for JAXB unmarshalling exception Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom error page for JAXB unmarshalling exception

    I use a marshaller bean:

    Code:
    <bean id="xmlMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> 
        <property name="contextPath" value="project.model" />
        <property name="schemas">
            <list>  
                <value>classpath:schemas/common-0.1.xsd</value>
                <value>classpath:schemas/contract-0.1.xsd</value>
                </value>    
            </list> 
        </property>
    </bean>
    and a message converter:

    Code:
    <bean id="marshallingHttpMessageConverter"        class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
        <property name="marshaller" ref="xmlMarshaller" />
        <property name="unmarshaller" ref="xmlMarshaller" />                
    </bean>
    In my controller I use @RequestBody to unmarshall the XML body of the request into my JAXB-generated classes. Since validation is enabled, whenever I receive an invalid XML I get an exception along these lines:

    Code:
    javax.servlet.ServletException: Unable to locate object to be marshalled in model: {exception=org.springframework.http.converter.HttpMessageNotReadableException: Could not read [class gr.opengov.agora.model.ContractsOXM]; nested exception is org.springframework.oxm.UnmarshallingFailureException: JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException
     - with linked exception:
    [org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'c:uniqueDocumentCodes'. One of '{"http://xfon.org/schema/common-0.1":uniqueDocumentCode}' is expected.]}
    This is fine, but I want to generate a custom error page (just for the unmarshalling exceptions) instead of the backtrace log. Is this possible?
    Thanks

  • #2
    Hello

    In my controller I use @RequestBody to unmarshall the XML body of the request into my JAXB-generated classes. Since validation is enabled, whenever I receive an invalid XML I get an exception along these lines:
    If such error message is thrown from your controller in a specific method, why not use a simple catch for org.xml.sax.SAXParseException within the method and use a new view name to your desired error page?

    Comment


    • #3
      The exception is thrown (and handled) by the framework before any code in the method is called.

      Comment

      Working...
      X