javax.xml.bind.JAXBException: class com.test.domain.HelloRequest nor any of its super class is known to this context.

Spring JIRA | Scott Frederick | 7 years ago
  1. 0

    When using MarshallingView with a Jaxb2Marshaller and more than one object in the model has JAXB annotations on it, automatic detection of the appropriate object in the model does not work as expected. Using this example MVC RESTful service: Request and Response classes: {code} @XmlRootElement(name = "request") public class HelloRequest { private String name; private String title; // getters and setters } @XmlRootElement(name = "response") public class HelloResponse { private String message; // getters and setters } {code} Controller: {code} @Controller @RequestMapping("/Hello") public class HelloController { @RequestMapping(value = "/greet", method = RequestMethod.GET) public HelloResponse greetGet(HelloRequest request) { String message = "Hello, " + request.getTitle() + " " + request.getName(); return new HelloResponse(message); } } {code} -servlet.xml {code} <beans xmlns=...> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="mediaTypes"> <map><entry key="xml" value="application/xml"/></map> </property> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.ContentTypeViewResolver" p:order="1" p:view-ref="xmlView"/> </list> </property> <property name="order" value="0"/> </bean> <bean id="xmlView" class="org.springframework.web.servlet.view.xml.MarshallingView"> <property name="marshaller" ref="marshaller"/> </bean> <oxm:jaxb2-marshaller id="marshaller"> <oxm:class-to-be-bound name="com.test.domain.HelloResponse"/> </oxm:jaxb2-marshaller> <bean name="helloMvc" class="com.test.hello.HelloController"/> </beans> {code} In this scenario, the Controller simply returns a response object. The model is automatically generated, and includes both the request and response objects (along with a few other objects). The model is passed to the MarshallingView, which tries to detect the appropriate model object for rendering the view (since no modelKey property was set on the MarshallingView). MarshallingView calls the supports() method of the configured Marshaller with each object in the model until supports() returns true. The detected object is then passed to the Marshaller for marshalling. When MarshallingView is used with Jaxb2Marshaller this causes a problem. Jaxb2Marshaller.supports(Class<?> c) will return true for any type that has a JAXB @XmlRootElement annotation on it, even if that Marshaller is not configured to support that class. In this scenario, MarshallingView encounters the request object in the model first and passes it to Jaxb2Marshaller.supports(), which returns true because the request object is annotated with @XmlRootElement. MarshallingView then passes the request object to Jaxb2Marshaller.marshal(). This call fails because the Marshaller is not configured to handle HelloRequest objects: {code}org.springframework.oxm.UncategorizedMappingException: Unknown JAXB exception; nested exception is javax.xml.bind.JAXBException: class com.test.domain.HelloRequest nor any of its super class is known to this context. at org.springframework.oxm.jaxb.Jaxb2Marshaller.convertJaxbException(Jaxb2Marshaller.java:580) at org.springframework.oxm.jaxb.Jaxb2Marshaller.marshal(Jaxb2Marshaller.java:412) at org.springframework.oxm.jaxb.Jaxb2Marshaller.marshal(Jaxb2Marshaller.java:395) at org.springframework.web.servlet.view.xml.MarshallingView.renderMergedOutputModel(MarshallingView.java:103) at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647) ... 37 more Caused by: javax.xml.bind.JAXBException: class com.test.domain.HelloRequest nor any of its super class is known to this context. at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:554) at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:470) at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:314) at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:243) at org.springframework.oxm.jaxb.Jaxb2Marshaller.marshal(Jaxb2Marshaller.java:408) ... 44 more {code} If the Jaxb2Marshaller were configured to support HelloRequest objects, then this exception would not be thrown but the request object would incorrectly be marshalled to the output view instead of the response object. To fix this, either the @XmlRootElement must be removed from the HelloRequest class, or the modelKey property must be set on the MarshallingView (to "helloResponse" in this case) to override automatic detection of the appropriate model object. Neither of these is a good solution. * The HelloRequest class does not need the @XmlRootElement annotation on it for this example, but in the real-world scenario this class is also used for other POST requests with the @RequestBody annotation. * Setting the modelKey property on the view would require a unique view bean to match each response object, since the name of the response object class is used to generate the model key.

    Spring JIRA | 7 years ago | Scott Frederick
    javax.xml.bind.JAXBException: class com.test.domain.HelloRequest nor any of its super class is known to this context.
  2. 0

    When using MarshallingView with a Jaxb2Marshaller and more than one object in the model has JAXB annotations on it, automatic detection of the appropriate object in the model does not work as expected. Using this example MVC RESTful service: Request and Response classes: {code} @XmlRootElement(name = "request") public class HelloRequest { private String name; private String title; // getters and setters } @XmlRootElement(name = "response") public class HelloResponse { private String message; // getters and setters } {code} Controller: {code} @Controller @RequestMapping("/Hello") public class HelloController { @RequestMapping(value = "/greet", method = RequestMethod.GET) public HelloResponse greetGet(HelloRequest request) { String message = "Hello, " + request.getTitle() + " " + request.getName(); return new HelloResponse(message); } } {code} -servlet.xml {code} <beans xmlns=...> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="mediaTypes"> <map><entry key="xml" value="application/xml"/></map> </property> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.ContentTypeViewResolver" p:order="1" p:view-ref="xmlView"/> </list> </property> <property name="order" value="0"/> </bean> <bean id="xmlView" class="org.springframework.web.servlet.view.xml.MarshallingView"> <property name="marshaller" ref="marshaller"/> </bean> <oxm:jaxb2-marshaller id="marshaller"> <oxm:class-to-be-bound name="com.test.domain.HelloResponse"/> </oxm:jaxb2-marshaller> <bean name="helloMvc" class="com.test.hello.HelloController"/> </beans> {code} In this scenario, the Controller simply returns a response object. The model is automatically generated, and includes both the request and response objects (along with a few other objects). The model is passed to the MarshallingView, which tries to detect the appropriate model object for rendering the view (since no modelKey property was set on the MarshallingView). MarshallingView calls the supports() method of the configured Marshaller with each object in the model until supports() returns true. The detected object is then passed to the Marshaller for marshalling. When MarshallingView is used with Jaxb2Marshaller this causes a problem. Jaxb2Marshaller.supports(Class<?> c) will return true for any type that has a JAXB @XmlRootElement annotation on it, even if that Marshaller is not configured to support that class. In this scenario, MarshallingView encounters the request object in the model first and passes it to Jaxb2Marshaller.supports(), which returns true because the request object is annotated with @XmlRootElement. MarshallingView then passes the request object to Jaxb2Marshaller.marshal(). This call fails because the Marshaller is not configured to handle HelloRequest objects: {code}org.springframework.oxm.UncategorizedMappingException: Unknown JAXB exception; nested exception is javax.xml.bind.JAXBException: class com.test.domain.HelloRequest nor any of its super class is known to this context. at org.springframework.oxm.jaxb.Jaxb2Marshaller.convertJaxbException(Jaxb2Marshaller.java:580) at org.springframework.oxm.jaxb.Jaxb2Marshaller.marshal(Jaxb2Marshaller.java:412) at org.springframework.oxm.jaxb.Jaxb2Marshaller.marshal(Jaxb2Marshaller.java:395) at org.springframework.web.servlet.view.xml.MarshallingView.renderMergedOutputModel(MarshallingView.java:103) at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647) ... 37 more Caused by: javax.xml.bind.JAXBException: class com.test.domain.HelloRequest nor any of its super class is known to this context. at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:554) at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:470) at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:314) at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:243) at org.springframework.oxm.jaxb.Jaxb2Marshaller.marshal(Jaxb2Marshaller.java:408) ... 44 more {code} If the Jaxb2Marshaller were configured to support HelloRequest objects, then this exception would not be thrown but the request object would incorrectly be marshalled to the output view instead of the response object. To fix this, either the @XmlRootElement must be removed from the HelloRequest class, or the modelKey property must be set on the MarshallingView (to "helloResponse" in this case) to override automatic detection of the appropriate model object. Neither of these is a good solution. * The HelloRequest class does not need the @XmlRootElement annotation on it for this example, but in the real-world scenario this class is also used for other POST requests with the @RequestBody annotation. * Setting the modelKey property on the view would require a unique view bean to match each response object, since the name of the response object class is used to generate the model key.

    Spring JIRA | 7 years ago | Scott Frederick
    javax.xml.bind.JAXBException: class com.test.domain.HelloRequest nor any of its super class is known to this context.
  3. 0

    Exception while Transforming message using Spring integration Marshaller

    Stack Overflow | 2 years ago | user3233451
    javax.xml.bind.JAXBException: class java.util.LinkedHashMap nor any of its super class is known to this context.]
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Spring-Integration Marshallling Exception

    Stack Overflow | 2 years ago | user3233451
    javax.xml.bind.JAXBException: class java.util.LinkedHashMap nor any of its super class is known to this context.]</strong>
  6. 0

    MarshallingWebServiceOutboundGateway with JAXB marshaller throws error

    Stack Overflow | 2 years ago
    javax.xml.bind.JAXBException: class org.springframework.util.LinkedMultiValueMap nor any of its super class is known to this context.

    2 unregistered visitors
    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. javax.xml.bind.JAXBException

      class com.test.domain.HelloRequest nor any of its super class is known to this context.

      at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getBeanInfo()
    2. Java RT
      MarshallerImpl.marshal
      1. com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:554)
      2. com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:470)
      3. com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:314)
      4. com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:243)
      4 frames
    3. Spring Object/XML Marshalling
      Jaxb2Marshaller.marshal
      1. org.springframework.oxm.jaxb.Jaxb2Marshaller.marshal(Jaxb2Marshaller.java:408)
      2. org.springframework.oxm.jaxb.Jaxb2Marshaller.convertJaxbException(Jaxb2Marshaller.java:580)
      3. org.springframework.oxm.jaxb.Jaxb2Marshaller.marshal(Jaxb2Marshaller.java:412)
      4. org.springframework.oxm.jaxb.Jaxb2Marshaller.marshal(Jaxb2Marshaller.java:395)
      4 frames
    4. Spring MVC
      FrameworkServlet.processRequest
      1. org.springframework.web.servlet.view.xml.MarshallingView.renderMergedOutputModel(MarshallingView.java:103)
      2. org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
      3. org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060)
      4. org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
      5. org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
      6. org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
      6 frames