JAXB Exception: Class not known to this context

ABK07 picture ABK07 · Jul 13, 2010 · Viewed 100.1k times · Source

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() { }
    ...
}

Answer

lexicore picture lexicore · Jul 13, 2010

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