簡體   English   中英

在Java客戶端中使用@POST請求發送參數

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

我正在通過Maven Web應用程序中的JDBC連接到SQL數據庫。 我創建了一個restfull API。

我也有一個Java客戶端,它連接到我的URI入口點並返回輸出

如果uri沒有參數,我可以通過客戶端成功返回數據,例如... / users / getAll

我在將參數傳遞給服務器上的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 /中,因此將不勝感激。

請嘗試以下格式。

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