3

我通过 post 命令将对象从客户端发送到服务器。当 GSON 返回时,它错误地在返回的字符串的开头和结尾添加了引号。

这是代码:

客户端代码:

public void getUserRole(){
    Form form = new Form();
    form.add("username", "Testuser");
    form.add("firstname", "Test");
    form.add("lastname", "User");
    form.add("userGroup", "Admin");
    form.add("userGroup", "Normal");

    String userGroupStr = "";

    try {
        userGroupStr = client.post("/rest/login/", "getUserRole", MediaType.APPLICATION_FORM_URLENCODED, String.class, form), 

        //Inspection of userGroupStr  object has role set to: "Admin" when it returns
    }
}

public <T> T post(final String uriExtension, final String path, final String mediaType, final Class<T> returnType,
    final Object postData) {
    return post(uriExtension, path, mediaType, returnType, null, postData);
}

服务器代码:

@POST
@Path("getUserRole")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@PermitAll
public String getUserRole(@Context HttpServletRequest req,
    @FormParam("username") String username,
    @FormParam("firstname") String firstname,
    @FormParam("lastname") String lastname,
    @FormParam("userGroup") List<String> userGroups) throws Throwable {

    //Stripped out code which essentially sets role to Admin for testing purposes
    String role = "Admin";

    //Inspection of object has role set to: Admin
    return role;
}

如果有人能告诉我如何阻止它为返回的对象添加一对额外的引号,那将不胜感激!

谢谢,

4

0 回答 0