users@jersey.java.net

Take 2: Using MessageBodyWriters as Providers for an AccountList and an Account object

From: John O'Conner <john_at_joconner.com>
Date: Fri, 31 Oct 2008 01:16:12 -0700

Sorry for the previous unfinished post. Let's try again...

Hi, I'm benefiting from all the great responses that people provide on
this list. I hope I can provide the same one day when I become more
skilled with this particular API. In the meantime, I have another
question.

In a nutshell, I'm able to create a summary list of "accounts" and
return that summary as xml. However, I cannot retrieve an individual
account. This problem appeared after I began experimenting with
Provider annotations and MessageBodyWriter. The "AccountsResource"
class seems to work well with its "Provider" to retrieve a summary
list of accounts (/accounts). Unfortunately, when I try to retrieve an
individual account (/accounts/{id}), I get an exception. The exception
is this:
java.lang.ClassCastException: xxx.xxx.AccountInfoDTO
        xxx.xxx.AccountsProvider.getSize(AccountsProvider.java:1)
        com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:242)

???Why is AccountsProvider.getSize() called? I want it to pass
requests to the sub-resource locator getAccount()...I'm confused
obviously. If you can help set me straight on what I'm missing, please
let me know!

I have 4 pertinent classes, and I've provided their most important
methods below:
* AccountsResource (provides an AccountSummaryList or an individual
AccountInfoDTO resource, which are not shown here)
* AccountsProvider (creates the xml for AccountSummaryList)
* AccountResource (supposed to be a sub-resource implementation used
from AccountsResource)
* AccountProvider (creates the xml for an AccountInfoDTO)



public class AccountsResource {

...
    @GET
    public AccountSummaryList getAccounts() {
        AccountSummaryList accountList = null;
   ...
     return accountList;
    }

        @Path("{id}")
        public AccountResource getAccount(@PathParam("id") long id) {
                return new AccountResource(accountManager, id);
        }
}

----------------------
@Provider
@Produces("text/xml")
public class AccountsProvider implements MessageBodyWriter<AccountSummaryList> {

        public long getSize(AccountSummaryList arg0, Class<?> arg1, Type
arg2, Annotation[] arg3, MediaType arg4) {
                return -1L;
        }

        public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2,
                        MediaType arg3) {
                return true;
        }

        @SuppressWarnings("unchecked")
        public void writeTo(AccountSummaryList accountList, Class<?> type,
Type genericType,
                        Annotation[] annotations, MediaType mediaType,
                        MultivaluedMap<String, Object> httpHeaders,
                        OutputStream entityStream) throws IOException,
                        WebApplicationException {

...
}

----------------------------

public class AccountResource {

...

        @GET
        @Produces("text/xml")
        public AccountInfoDTO getAccount() {
                AccountInfoDTO account = null;
...
                return account;
        }
}

-------------------------------------------

@Provider
@Produces("text/xml")
public class AccountProvider implements MessageBodyWriter<AccountInfoDTO> {

        public long getSize(AccountInfoDTO arg0, Class<?> arg1, Type arg2,
Annotation[] arg3, MediaType arg4) {
                return -1L;
        }

        public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2,
                        MediaType arg3) {
                return true;
        }

        @SuppressWarnings("unchecked")
        public void writeTo(AccountInfoDTO account, Class<?> type, Type genericType,
                        Annotation[] annotations, MediaType mediaType,
                        MultivaluedMap<String, Object> httpHeaders,
                        OutputStream entityStream) throws IOException,
                        WebApplicationException {

...
}