GSON为返回的String对象添加引号

时间:2012-12-03 13:54:35

标签: java gson

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

这是代码:

客户代码:

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;
}

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

谢谢,

0 个答案:

没有答案