简体   繁体   English

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

[英]Sending parameters with a @POST request in java client

I'm connecting to a SQL db via JDBC in a maven web app. 我正在通过Maven Web应用程序中的JDBC连接到SQL数据库。 I've created a restfull API. 我创建了一个restfull API。

I also have a java client which connects to my URI entry points and returns the output 我也有一个Java客户端,它连接到我的URI入口点并返回输出

i can successfully return data through the client if the uri has no params eg .../users/getAll 如果uri没有参数,我可以通过客户端成功返回数据,例如... / users / getAll

I'm having some trouble with passing params to my post method on the server. 我在将参数传递给服务器上的post方法时遇到了一些麻烦。

Server code 服务器代码

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

Client code 客户代码

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

I may be going about passing the parameters incorrectly,with the string input? 我可能正在使用字符串输入来错误地传递参数? As the data is not being populated into the DB/ any advice would be appreciated. 由于未将数据填充到DB /中,因此将不胜感激。

Please try the below format. 请尝试以下格式。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM