1

I have created a CN1 web service which some custom objects that I want to externalize in order to send over the network. I read through several articles on how to create the web service and how to work with the CN1 Externalizable interface.

This works well for web service methods that return a custom externalizable object, however the only indicator that I have is that a method which takes an externalizable object as an argument, I get the following error:

SCHWERWIEGEND: Servlet.service() for servlet  [CN1WebServiceServlet] 
in context with path [/<myPath>] threw exception
java.io.IOException: Object type not supported: Post

The object is properly registered with the Util class, as changing either the object ID or commenting out the register call will cause a null pointer instead of the IO exception.

The Post class looks like this (simplified to the minimum which already fails):

public class Post implements Externalizable {
public int postid;
public int userid;

// default constructor needed for web service marshalling
public Post() {

}

@Override
public int getVersion() {
    return 1;
}

@Override
public void externalize(DataOutputStream out) throws IOException {
    Util.writeUTF("" + postid, out);
    Util.writeUTF("" + userid, out);
}

@Override
public void internalize(int version, DataInputStream in) throws IOException {
    this.postid = Integer.parseInt(Util.readUTF(in));
    this.userid = Integer.parseInt(Util.readUTF(in));
}

@Override
public String getObjectId() {
    return "Post";
}

Note that this Post object works well when I call a web service method which returns a post object, but not when I send a Post object to the web service:

// works
public static com.codename1.io.Externalizable getPostDetails(int postid) {
   return getPostDetails(postid);
}

// fails
public static void sendPost(com.codename1.io.Externalizable post) {
    sendPost(post);
}

I am at a loss of what I missed here.

Thanks and best regards

Lequi
  • 539
  • 2
  • 13

2 Answers2

2

In your Servlet code call Util.register("Post", Post.class); which should hopefully resolve this.

Shai Almog
  • 51,749
  • 5
  • 35
  • 65
0

Thanks a lot Shai! My mistake was to assume that registering the externalizable object on one side only. But of course it needs to be registered wherever it is going to be internalized, so in this case on my server.

Solution: Within the "CN1WebServiceServlet" (not the ProxyServer class where the rest of the code has to be completed), call Util.register("Post", Post.class);

      if(methodName.equals("sendPost")) {
            Util.register("Post", Post.class); // this is a my insertedline, rest is generated
            Object[] args = ProxyServerHelper.readMethodArguments(di, def_sendPost);
            WebServiceProxyServer.sendPost((com.codename1.io.Externalizable)args[0]);
            ProxyServerHelper.writeResponse(response, def_sendPost);
            return;
        }
Lequi
  • 539
  • 2
  • 13