我尝试使用restygwt进行服务器 - 客户端通信。因此,我有一个服务接口(客户端)和一个控制器(服务器)。
@Path("/user")
public interface UserAdminRestService extends RestService {
@GET
@Path("/users")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public void getUsers(MethodCallback<List<GWTUser>> methodCallback);
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public void getUser(Integer id, MethodCallback<GWTUser> callback);
}
@Controller
@RequestMapping(value ="/user")
public class AdminSpringController {
@RequestMapping(value = "/users", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody List<GWTUser> getUsers() {
...
}
@RequestMapping( value = "/{id}", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody GWTUser getUser(@PathVariable Integer id) throws NotFoundException { ...
}
}
为了获得具体数据,我制作了一个MethodCall:
Defaults.setServiceRoot(GWT.getHostPageBaseURL());
UserAdminRestService userService = GWT.create(UserAdminRestService.class);
((RestServiceProxy)userService).setResource(new Resource("/user"));
userServiceClient.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) {
view.setUserList(response);
}
});
但是当我尝试构建我的应用程序时,我收到了错误消息:
[INFO] Compiling module my.app.web
[INFO] Computing all possible rebind results for 'my.app.client.UserAdminRestService'
[INFO] Rebinding my.app.client.UserAdminRestService
[INFO] Invoking generator org.fusesource.restygwt.rebind.RestServiceGenerator
[INFO] Generating: my.app.client.UserAdminRestService_Generated_RestServiceProxy_
[INFO] [ERROR] Content argument must be a class.
[INFO] [ERROR] Errors in 'my/app/admin/client/activity/UserListActivity.java'
[INFO] [ERROR] Line 69: Failed to resolve 'my.app.client.UserAdminRestService' via deferred binding
[INFO] [WARN] For the following type(s), generated source was never committed (did you forget to call commit()?)
[INFO] [WARN] my.app.client.UserAdminRestService_Generated_RestServiceProxy_
第69行:UserAdminRestService userService = GWT.create(UserAdminRestService.class);
我不知道自己做错了什么。感谢任何想法或帮助:)
编辑:
GWTUser
公共类GWTUser {/**
* Certain user entity id. Only for representation, can not be
* persisted into the database.
*/
private int id;
/**
* Certain user id.
*/
private String userId;
/**
* user password.
*/
private String password;
/**
* Represents a certain status if user is enabled.
*/
private boolean enabled = true;
/**
* Represents a certain status if user is locked.
*/
private boolean locked = false;
/**
* user e-mail.
*/
private String email;
/**
* full user name.
*/
private String name;
/**
* the version of the real user entity. Only for representation, can not be
* persisted into the database.
*/
private int version;
// Getter and Setter
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public boolean isLocked() {
return locked;
}
public void setLocked(boolean locked) {
this.locked = locked;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((userId == null) ? 0 : userId.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
GWTUser other = (GWTUser) obj;
if (userId == null) {
if (other.userId != null)
return false;
} else if (!userId.equals(other.userId))
return false;
return true;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("User [userId=");
builder.append(userId);
builder.append(", name=");
builder.append(name);
builder.append(", email=");
builder.append(email);
builder.append(", password=");
builder.append(password != null ? "****" : "<not set>");
builder.append(", enabled=");
builder.append(enabled);
builder.append(", locked=");
builder.append(locked);
builder.append("]");
return builder.toString();
}
}
修改
gwt.gwt.xml file containing all the gwt code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module SYSTEM "gwt-module.dtd">
<module>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name='com.google.gwt.user.User' />
<inherits name='com.google.gwt.activity.Activity'/>
<inherits name='com.google.gwt.place.Place' />
<inherits name='org.fusesource.restygwt.RestyGWT' />
<!-- Use ClientFactoryImpl by default -->
<replace-with class="my.app.client.factory.ClientFactoryImpl">
<when-type-is class="my.app.client.factory.ClientFactory"/>
</replace-with>
<!--Specify the app entry point class. -->
<entry-point class='my.app.client.AdminEntryPoint'/>
<source path='rest'/>
<source path='client'/>
<source path='consts'/>
</module>
和web.gwt.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module SYSTEM "gwt-module.dtd">
<module rename-to="web">
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name='com.google.gwt.user.User' />
<inherits name='org.fusesource.restygwt.RestyGWT' />
<inherits name='my.app.gwt' />
</module>
我认为由于两个模块存在问题,因为接口UserAdminRestService
和GWTUser
仍然相同。
答案 0 :(得分:1)
问题不在于GWTUser,而是Payload,意思是:
当我尝试getUsers(MethodCallback callback)
它工作正常但getUser(int id, MethodCallback callback)
我打印错误,因为int
不是一个类。因此,我将 int 更改为 Integer ,它运行正常。
答案 1 :(得分:0)
在您粘贴的代码中,您有一个&#39;在第一个{在UserAdminRestService
之后现在如果问题不是这样,请粘贴您的GWTUser类