我正在尝试使用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_


LINE 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>


我认为由于有两个模块,因此存在问题,因为接口UserAdminRestServiceGWTUser仍然相同。

最佳答案

问题不是GWTUser,而是有效载荷,这意味着:

当我尝试使用getUsers(MethodCallback callback)时,它可以正常工作,但使用getUser(int id, MethodCallback callback)时,我会打印错误,因为int不是类。因此,我将int更改为Integer,并且工作正常。

10-05 22:09