Hi John,
The problem is with the isWritable methods:
public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2,
MediaType arg3) {
return true;
}
You need to do this:
public class AccountsProvider ...
...
public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2,
MediaType arg3) {
return AccountSummaryList.class == arg0;
}
}
public class AccountProvider ...
...
public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2,
MediaType arg3) {
return AccountInfoDTO.class == arg0;
}
}
even though the writers are using generics it is still necessary to do
the checking in the isWriteable method.
The runtime is getting confused and is trying to invoke a method on
the AccountsProvider with an instance of AccountInfoDTO, hence the
cast exception.
Paul.
On Oct 31, 2008, at 9:16 AM, John O'Conner wrote:
> 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 {
>
> ...
> }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>