简体   繁体   中英

A message body writer for Java type, class bookInfoListType, and MIME media type application/xml was not found

I have been browsing stackoverflow for this error lately and I'm unable to find a solution on almost all the threads that I have been to. That's why I'm posting this question here.

The problem is that I'm having said error while returning the response. Here is my XSD definition:

<xs:element name="bookInfoList">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="bookInfo" type="bookInfoType" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="bookInfo" type="bookInfoType"/>

<xs:complexType name="bookInfoListType">
    <xs:sequence>
        <xs:element name="bookInfo" type="bookInfoType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="bookInfoType">
    <xs:sequence>
        <xs:element name="bookId" type="xs:string" minOccurs="1" maxOccurs="1"/>
        <!-- ... more elements !-->
    </xs:sequence>
</xs:complexType>

which generated the following bookListInfoType class

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "bookInfoListType", propOrder = {
    "bookInfo"
})
public class bookInfoListType {

    protected List<bookInfoType> bookInfo;

    public List<bookInfoType> getbookInfo() {
        if (bookInfo == null) {
            bookInfo = new ArrayList<bookInfoType>();
        }
        return this.bookInfo;
    }
}

But when I try to send the response back like

return Response.status(HttpStatus.SC_OK).entity(bookInfoListConverter.convert(wsContext, allBooks)).build();

I get the said error.

In my ObjectFactory , I do see

public bookInfoListType createbookInfoListType() {
    return new bookInfoListType();
}

Here is my REST method:

@Path ("v1/storename/")
public class BookResource
{
    @GET
    @Path ("books/{book_id}/info.xml")
    @Produces (MediaType.APPLICATION_XML)
    public Response getBookInfoXML () {
        //business logic
        return Response.status(HttpStatus.SC_OK).entity(bookInfoListConverter.convert(wsContext, allBooks)).build();
    }
}

Not to mention, I have another JAXB generated class called bookInfoList as you can see in the XSD. Their definition is almost identical (this is one thing I'm suspecting which could be the problem, but rest of my classes which are following same pattern are working fine).

I am still trying to bang my head around to figure out whats going on but I'm running out of ideas, any help would be appreciated.

An entity returned from the resource method in the form of an arbitrary Java object can be serialized by Jersey into a container output stream as a specified representation. Of course, while JAX-RS implementations do provide default support for most common combinations of Java type and it's respective on-the-wire representation formats, JAX-RS implementations do not support the conversion described above for any arbitrary Java type and any arbitrary representation format by default.

For every Java type and content type combination your application uses (which is not covered by the default providers that come with the JAX-RS implementation) you must have a provider that knows how to handle the combination, so you must have appropriate MessageBodyWriter and MessageBodyReader classes (see the link above for how to write them if you should need to - you might want to refer to the exact documentation for the JAX-RS implementation/version you are using).

With that said, I find your error a little bit strange since Jersey already contains default support for entity providers that can marshall JAXB beans. You might want to compare your service against a tutorial (eg Using JAX-RS With JAXB ) to make sure the error you posted is not a red herring

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM