0

Aloha.

I have a Restlet server application with a @Get method that is supposed to be returning Object[], but the client receives an Object[] with elements of type LinkedHashMap. Each of the received array's elements correspond to the respective elements of the objects that the server was supposed to send.

Here is the relevant part of my resource (interface) file:

@Get
public Object[] retrieve() ;

Here is the relevant portion of my server resource file:

public Object[] retrieve()
{

  User newUser = new User() ;
  newUser.firstName = "John" ;
  newUser.lastName = "Doe" ;

  Object[] objectArray = (Object[]) Array.newInstance( User.class, 1 ) ;
  objectArray[0] = newUser ;

  return objectArray ;

}

Here is the relevant portion of my client:

public static void main( String[] args ) throws Exception
{
  Object[] arrayOfObjects = (Object[]) clientResource.retrieve() ;   //  array of LinkedHashMap elements
}

I assume that obtaining the result that I desire to obtain is easily done, but I am still very new to the Restlet framework. Thus, can someone tell me what I am doing wrong and show me an example of how to correctly send an array of (JSON encoded) objects from the server?

Thanks, in advance.

Take care,
Darwin

==================================================================================

UPDATE

Per StaxMan's comment (below), I also tried returning a generic List, but this just resulted in an ArrayList of LinkedHashMap elements, not an ArrayList of the desired type of elements.

Here is the relevant part of my resource (interface) file:

@Get
public <T> List<T> retrieve() ;

Here is the relevant portion of my server resource file:

public <T> List<T> retrieve()
{
  List<T> list = ... ;
  return list ;
}

Here is the relevant portion of my client:

//  the following does not work, since the returned ArrayList's
//  elements are not User objects but LinkedHashMap objects:
User[] users = (User[]) ((ArrayList<User>)clientResource.<User>retrieve()).toArray( (User[])Array.newInstance(User.class,0) ) ;

Can anyone tell me how to obtain an Array or List or ArrayList of the objects that I want from the server, while still maintaining the generic (or even pseudo generic) nature of the server?

Thanks, in advance.

1
  • You can not use type variables like 'T'. That is not a type, that's just a place holder. You must pass a real type, like User[].class for array of User objects; type variable will not do since it is only used by compiler, not retained during runtime.
    – StaxMan
    Feb 26, 2013 at 5:32

1 Answer 1

0

This is because a List is a java.lang.Object -- you asking for a List of Objects and that you are getting. Without additional type information, that is pretty much all you could possibly get. In your case, you should rather return User[] (or List<User>)

6
  • Thanks for the response. I am actually trying to return a pseudo generic array (or List or ArrayList or whatever) of objects, since Restlet does not yet seem capable of returning an actually generic array (or List or ArrayList or whatever). Thus, I was trying to compromise. Anyway, I made analogous changes to the ones that you suggested, but using generics (not a concrete List element type), and it is now returning an ArrayList of LinkedHashMap elements. I will post the details to the original question, above... Feb 7, 2013 at 0:20
  • Ok. In that case, you need to enable polymorphic type support, so that deserializer knows the runtime type, since it is not available from information you are passing. If you had common super-type for values (T extends MyBaseType), you could use @JsonTypeInfo. But if not, probably need to enable so-called "default typing". This is done with ObjectMapper.enableDefaultTyping(...). Either way, Jackson will then include type information on serialization, and use it for deserializing into actual type.
    – StaxMan
    Feb 7, 2013 at 16:10
  • Thanks, StaxMan. Can you please provide a simple, working example of what you are suggesting, by any chance? Feb 26, 2013 at 0:22
  • I think you need to ask Restlet list: I don't know what client.retrieve() is expected to return.
    – StaxMan
    Feb 26, 2013 at 5:34
  • StaxMan, client.retrieve() is just my server's @GET method... Would you happen to have an example of the T extends MyBaseType, etc. approach that you mentioned? Feb 28, 2013 at 0:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.