我正在尝试通过我的 Restservice 获取数据。因此我有我的弹簧控制器和我的休息服务。
@Path("/user")
public interface UserAdminRestService extends RestService {
public static class Util {
private static UserAdminRestService instance;
/**
* Returns an instance of the {@link UserAdminRestService}.
* @return an instance of the UserAdminRestService.
*/
public static UserAdminRestService getUserAdminRestService() {
Resource resource = new Resource(GWT.getModuleBaseURL() + "user");
GWT.log("UserAdmin-Rescource-Path: "+resource.getPath());
if (instance == null ) {
instance = GWT.create(UserAdminRestService.class);
}
((RestServiceProxy) instance).setResource(resource);
return instance;
}
}
@GET
@PATH("/test")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public void getUsers(MethodCallback<List<GWTUser>> callback);
}
GWTUser 代表我的用户实体。
public class GWTUser {
public interface GWTUserJED extends JsonEncoderDecoder<GWTUser> {
}
private String userId;
... // data
@Override
public String toString() {
if (GWT.isClient()) {
GWTUserJED jed = GWT.create(GWTUserJED.class);
return jed.encode(this).toString();
}
return super.toString();
}
}
我试图在我的 gwt 应用程序中调用它:
HashMap<String, String> header = new HashMap<String, String>();
header.put(Resource.HEADER_ACCEPT, Resource.CONTENT_TYPE_JSON+"; charset=utf-8");
Resource resourceGetUsers = new Resource("/user/test", header);
UserAdminRestService.Util.getUserAdminRestService().getUsers(new MethodCallback<List<GWTUser>>() {
@Override
public void onFailure(Method method, Throwable exception) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(Method method, List<GWTUser> response) {
// TODO Auto-generated method stub
}
});
但是当我尝试编译我的应用程序时收到一条错误消息。
--- gwt-maven-plugin:2.5.1:compile (default-cli) @ myapp-admin-gwt ---
auto discovered modules [com.myapp.admin.gwt]
Compiling module com.myapp.admin.gwt
Computing all possible rebind results for 'com.myapp.admin.rest.UserAdminRestService'
Rebinding com.myapp.admin.rest.UserAdminRestService
Invoking generator org.fusesource.restygwt.rebind.RestServiceGenerator
Generating: com.myapp.admin.rest.UserAdminRestService_Generated_RestServiceProxy_
Generating: com.google.gwt.http.client.Response_Generated_JsonEncoderDecoder_
[ERROR] Abstract classes must be annotated with JsonTypeInfo
[ERROR] Errors in 'com/myapp/admin/rest/UserAdminRestService.java'
[ERROR] Line 53: Failed to resolve 'com.myapp.admin.rest.UserAdminRestService' via deferred binding
[WARN] For the following type(s), generated source was never committed (did you forget to call commit()?)
[WARN] com.myapp.admin.rest.UserAdminRestService_Generated_RestServiceProxy_
[WARN] com.google.gwt.http.client.Response_Generated_JsonEncoderDecoder_
所以我不确定该怎么做。我的服务是否需要 JsonTypeInfo,但有什么用?!或者我是否需要让我的 GWTUser 抽象并添加一个 JsonTypeInfo。谢谢你的帮助。