[英]java rest web service using post
我正在嘗試使用“ POST”方法來做一個java rest Web服務。我調用該Web服務的客戶端部分工作正常。但是我在通過“ POST”方法訪問傳遞的參數時遇到了困難。任何幫助都是可取的。 這是我的客戶
public static void main(String[] args) throws IOException
{
String urlParameters = "param1=world¶m2=abc¶m3=xyz";
String request = "http://localhost:8080/wsRevDash/rest/post/test";
URL url = new URL(request);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("charset", "utf-8");
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
for (int c; (c = in.read()) >= 0;)
System.out.print((char)c);
}
這是我的java rest網絡服務方法,用於訪問參數(無法訪問)。
@POST
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public String getpostdata(@QueryParam("param1") String param1,@QueryParam("param2") String param2)
{
JSONObject jObjDevice = new JSONObject();
jObjDevice.put("Hello",param1);
return jObjDevice.toJSONString();
}
運行時,我將{“ Hello”:null}作為json字符串而不是{“ Hello”:“ world”}。獲取null表示訪問傳遞的參數是不道德的。請提供幫助。
您可以使用@QueryParam,如下所示。
public String getpost( @QueryParam("param1") String param1,
@QueryParam("param2") String param2){
// Access both param below
}
使用POST請求發送數據非常簡單。
代替conn.getOutputStream().write(postDataBytes);
您將必須使用DataOutputStream
發送數據
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public static void main(String[] args) throws IOException
{
Map<String, String> params = new LinkedHashMap<String, String>();
params.put("param1", "hello");
params.put("param2", "world");
JSONObject myJSON = new JSONObject(params);
System.out.println(myJSON);
byte[] postData = myJSON.toString().getBytes(StandardCharsets.UTF_8);
int postDataLength = postData.length;
String request = "http://localhost:8080/wsRevDash/rest/post/test";
URL url = new URL(request);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
//Try with Resources Example, just giving you an option
// try (DataOutputStream wr = new
// DataOutputStream(conn.getOutputStream()))
// {
// wr.write(postData);
// }
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.write(postData);
}
注意:您在請求中發送了application/json
標頭,但是我在代碼中看不到JSON。 建議僅發送有用的標題
您可以像這樣直接將HashMap
轉換為JSONObject
,
org.json.JSONObject jsonObject = new org.json.JSONObject(params);
但這僅適用於Map<String, String>
要訪問Web服務中的參數,您必須接受JSONObject
而不是Map<String, String>
。
public String getpost(JSONObject params) throws JSONException
{
if(params.has("param1"))
System.out.println(params.getString("param1"));
if(params.has("param2"))
System.out.println(params.getString("param2"));
//IMPLEMENT YOUR LOGIC HERE AND THEN RETURN STRING
return "your_return string";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.