在java客户端中使用@POST请求发送参数

时间:2016-12-19 16:05:33

标签: java rest maven parameters glassfish

我在maven web app中通过JDBC连接到SQL数据库。我创建了一个restfull API。

我还有一个java客户端连接到我的URI入口点并返回输出

如果uri没有参数,我可以通过客户端成功返回数据,例如... / users / getAll

我在将params传递给服务器上的post方法时遇到了一些麻烦。

服务器代码

@POST
@Path("/NewUser")
@Produces(MediaType.APPLICATION_JSON)
public List<String> CreateUser(@HeaderParam("Name")String   name,@HeaderParam("Email")String email)
{
    if(name.equals("")||email.equals(""))
          return null;
    else return BankingService.CreateUser(name, email);
}

客户端代码

     WebResource webResource = client.resource(url);

        String input = "{\"name\":tom,\"email\":\"tom@email.com\"}";

        ClientResponse response = webResource.type("application/json")
                .post(ClientResponse.class, input);

        String output = response.getEntity(String.class);
        System.out.println(output);

我可能会使用字符串输入错误地传递参数? 由于数据没有填充到DB /  任何建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

请尝试以下格式。

import org.apache.commons.io.FileUtils;
import java.nio.charset.StandardCharsets;

log.info("Previous value of variable_1 = " + vars.get("variable_1"));

List csvLines = FileUtils.readLines(new File("test.csv"), StandardCharsets.UTF_8);

for (String csvLine : csvLines) {
    if (csvLine.startsWith(vars.get("variable_1"))) {
        vars.put("variable_1", csvLine.split(",")[1]);
        break;
    }
}

log.info("New value of variable_1 = " + vars.get("variable_1"));
相关问题