46

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?

JAXBException occurred : class com.octory.ws.dto.ProfileDto nor any of its super class is known to this context...

Following are the service method and relevant DTOs:

public class Service {
   public Response results() {
   Collection<ProfileDto> profilesDto = new ArrayList<ProfileDto>();
   ...
   SearchResultDto srd = new SearchResultDto();
   srd.setResultEntities(profilesDto); // Setting profilesDto collection as resultEntities
   srd.setResultSize(resultSize);
   return Response.ok(srd).build();
   }
}

SearchResultDto:

@XmlRootElement(name="searchResult")
public class SearchResultDto {
    private Collection resultEntities;
    private int resultSize;

    public SearchResultDto() { }

    @XmlElementWrapper(name="resultEntities")
    public Collection getResultEntities() {
        return resultEntities;
    }

    public void setResultEntities(Collection resultEntities) {
        this.resultEntities = resultEntities;
    }

    public int getResultSize() {
        return resultSize;
    }

    public void setResultSize(int resultSize) {
        this.resultSize = resultSize;
    }
}

ProfileDto:

@XmlRootElement(name="profile")
public class ProfileDto {
    ...
    ...
    public ProfileDto() { }
    ...
}
ABK07
  • 515
  • 1
  • 4
  • 7

6 Answers6

41

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

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Adding @XmlSeeAlso resolved the issue; I was under the impression the annotations was only needed when the referenced class was a sub-class. Thanks. – ABK07 Jul 13 '10 at 18:48
  • 4
    What if the class is `SearchResultDto` where T is generic ? – Hendy Irawan Aug 16 '12 at 22:30
  • 4
    @Hendy Irawan - The annotation can also be added to the web service itself (i.e. after the `@WebService` annotation). If you are dealing with generics it makes more sense to declare additional types there as by that stage you should know the full set of types. – CurtainDog Apr 29 '13 at 05:44
  • @CurtainDog: Thanks for the tip about adding `@XmlSeeAlso` to the webservice itself. I'd been having problems with generated types and adding `@XmlSeeAlso` to the generated classes didn't seem to help, but adding that annotation to the webservice cleared up the problem. – FrustratedWithFormsDesigner Jul 04 '13 at 17:55
  • @CurtainDog very useful comment. Btw, is there any alternative use of `@XMlSeeAlso` in `JAX-RS` annotated resources? – kosta Jan 22 '16 at 16:20
  • 1
    In my case, the ObjectFactory class did not have a 'create' method for my class. I had to add the method and it fixed the error. While working with Jaxb, as a rule of thumb I always check package-info.java and ObjectFactory.java when such errors are encountered. – imnd_neel Jan 25 '17 at 08:29
  • Worked for me too , in case of multiple classes use like : @XmlSeeAlso({ClassOne.class,ClassTwo.class,....}) – Shashank Bodkhe Jan 21 '21 at 17:53
  • Thank you imnd_neel. This piece of info saved us. In our case, an abstract class had a "child" implementation class. We only had a create method that returned the abstract class from our ObjectFactory. By adding a create method that returns a new implementation class, it solved the problem. Unfortunately, by searching our code base, it would seem that the new create method we added is never used, and is at risk of being deleted by future automated code reviewers, so we added a big comment: do not delete this method! – AdamE Aug 02 '22 at 16:46
40

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

JAXBContext context = JAXBContext.newInstance(MyRootXmlClass.class);
user64141
  • 5,141
  • 4
  • 37
  • 34
9

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;
}
Yevhen Surovskyi
  • 931
  • 11
  • 19
5

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.

Apurv
  • 3,723
  • 3
  • 30
  • 51
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
2

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>
Monica
  • 21
  • 1
1

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

wattostudios
  • 8,666
  • 13
  • 43
  • 57
Slava
  • 11
  • 1