Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB Exception: Class not known to this context

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() { }     ... } 
like image 636
ABK07 Avatar asked Jul 13 '10 13:07

ABK07


People also ask

What is JAXB exception?

This exception indicates that an error has occurred while performing an unmarshal operation that prevents the JAXB Provider from completing the operation. class. ValidationException. This exception indicates that an error has occurred while performing a validate operation.

What is JAXB context?

The JAXBContext instance is initialized from a list of colon separated Java package names. Each java package contains JAXB mapped classes, schema-derived classes and/or user annotated classes. Additionally, the java package may contain JAXB package annotations that must be processed.

What is javax XML bind MarshalException?

A javax.xml.bind.MarshalException error may occur when a null object is used as an argument to a java.util.List parameter on a JAX-WS web method.

What is @XmlRootElement?

Annotation Type XmlRootElementMaps a class or an enum type to an XML element. Usage. The @XmlRootElement annotation can be used with the following program elements: a top level class. an enum type.


2 Answers

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

like image 131
lexicore Avatar answered Sep 25 '22 01:09

lexicore


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

JAXBContext context = JAXBContext.newInstance(MyRootXmlClass.class); 
like image 36
user64141 Avatar answered Sep 21 '22 01:09

user64141