使用post方法的JavaRESTWeb服务
我正在尝试使用“POST”方法执行java rest web服务。我调用web服务的客户端部分工作正常。但我在通过“POST”方法访问传递的参数时遇到了困难。任何帮助都是值得赞赏的。这是我的客户使用post方法的JavaRESTWeb服务,java,Java,我正在尝试使用“POST”方法执行java rest web服务。我调用web服务的客户端部分工作正常。但我在通过“POST”方法访问传递的参数时遇到了困难。任何帮助都是值得赞赏的。这是我的客户 public static void main(String[] args) throws IOException { String urlParameters = "param1=world¶m2=abc"; String request = "http://
public static void main(String[] args) throws IOException
{
String urlParameters = "param1=world¶m2=abc";
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);
}
这是我的JavaRESTWeb服务
@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();
}
参数param1=world。但是当我运行web服务时,我得到的json字符串是{“Hello”:null},而不是{“Hello”:“world”}。请建议进行任何更改。我认为问题在于注释i.e@QueryParam。如果是这种情况,请提供使用正确注释访问参数的正确方法 请添加
@Consumes(MediaType.APPLICATION_JSON)
注释,并为其提供一个JSON对象,而不是两个字符串作为post方法参数。因此,无法使用不同的字符串作为post方法参数运行web服务。请有人帮助我这样做,因为这很简单,如果可能的话。请添加@Consumes(MediaType.APPLICATION_JSON)
注释,并为其提供一个JSON对象,而不是两个字符串作为post方法参数。因此,无法使用不同的字符串作为post方法参数来运行web服务。如果可能,请有人帮助我这样做,因为这很简单。