JAXB Exception: Class not known to this context

后端 未结 6 1030
-上瘾入骨i
-上瘾入骨i 2020-11-29 04:48

When I call a particular restful service method, which is built using CXF, I get the following error, anyone know why and how to resolve it?

相关标签:
6条回答
  • 2020-11-29 05:28

    I had the same problem with spring boot. It resolved when i set package to marshaller.

    @Bean
    public Jaxb2Marshaller marshaller() throws Exception
    {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setPackagesToScan("com.octory.ws.dto");
        return marshaller;
    }
    
    @Bean
    public WebServiceTemplate webServiceTemplate(final Jaxb2Marshaller marshaller)   
    {
        WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
        webServiceTemplate.setMarshaller(marshaller);
        webServiceTemplate.setUnmarshaller(marshaller);
        return webServiceTemplate;
    }
    
    0 讨论(0)
  • 2020-11-29 05:29

    Fixed it by setting the class name to the property "classesToBeBound" of the JAXB marshaller:

    <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
            <property name="classesToBeBound">
              <list>
                    <value>myclass</value>
              </list>
            </property>
    </bean>
    
    0 讨论(0)
  • 2020-11-29 05:31

    This error message happens either because your ProfileDto class is not registered in the JAXB Content, or the class using it does not use @XmlSeeAlso(ProfileDto.class) to make processable by JAXB.

    About your comment:

    I was under the impression the annotations was only needed when the referenced class was a sub-class.

    No, they are also needed when not declared in the JAXB context or, for example, when the only class having a static reference to it has this reference annotated with @XmlTransient. I maintain a tutorial here.

    0 讨论(0)
  • 2020-11-29 05:31

    I had the same exception on Tomcat.. I found another problem - when i use wsimport over maven plugin to generate stubs for more then 1 WSDLs - class ObjectFactory (stubs references to this class) contains methods ONLY for one wsdl. So you should merge all methods in one ObjectFactory class (for each WSDL) or generate each wsdl stubs in different directories (there will be separates ObjectFactory classes). It solves problem for me with this exception..J

    0 讨论(0)
  • 2020-11-29 05:40

    Your ProfileDto class is not referenced in SearchResultDto. Try adding @XmlSeeAlso(ProfileDto.class) to SearchResultDto.

    0 讨论(0)
  • 2020-11-29 05:53

    I had this error because I registered the wrong class in this line of code:

    JAXBContext context = JAXBContext.newInstance(MyRootXmlClass.class);
    
    0 讨论(0)
提交回复
热议问题