gwtrequestfactory

GWT 2.6 - RequestFactory Locator Could not find domain method similar to


I am new to GWT and using the version 2.6 with RequestFactory but I am running into GWT validation problems. Somehow GWT cannot find the method on the domain side once I started using Locator and LocatorService (before this, everything worked just find for me).

Below is my implementation:

My request factory looks like this:

public interface MyRequestFactory extends RequestFactory {
    MyRequest createRequest();

    @Service(value = MyModel.class, locator = MyModelServiceLocator.class)
    public interface MyRequest extends RequestContext {
        Request<MyModelProxy> find(String id);
    }
}

My service locator looks like this:

public class MyServiceLocator implements ServiceLocator {
    private static MyService serviceInstance;

    private static MyService getServiceInstance() {
        if (serviceInstance == null) {
            serviceInstance = new MyService();
        }
        return serviceInstance;
    }

    @Override
    public Object getInstance(Class<?> klass) {
        return MyServiceLocator.getServiceInstance();
    }
}

My service looks like:

public class MyService {
    public MyModel find(String id) {
        // implementations
    }
}

But GWT validation complains about not being able to find 'MyModel find(String)' on the domain side. But once I put this method into MyModel class (which I don't really want to), it works just fine.

I tried to google around and found couple of related posts but none of those problems actually applied to me. Can someone please help me?


Solution

  • If the methods in MyRequest have their domain counterparts in MyService, then @Service should point to MyService.class, not MyModel.class.

    The fact that you use a ServiceLocator tells RequestFactory that it should look for instance methods rather than static methods, and will use the ServiceLocator to get an instance of the service pointed to by @Service. In the case you had had a matching find method in MyModel (i.e. compile-time validation would have passed), then you'd have encountered an error at runtime because the MyService instance returned by your ServiceLocator cannot be cast to the MyModel that you refer to in your @Service annotation.