use*_*555 6 rest web-services put form-parameter
我开发了REST服务.我可以通过浏览器或客户端应用程序测试GET方法.但是那些有PUT方法的人我不知道如何通过浏览器来消费它们......
例如,在插入userId之后,我有这个方法打开灯:
@PUT
@Path("/lampon")
@Produces({"application/json", "text/plain"})
@Consumes("multipart/form-data")
public boolean turnOnLamp(@FormParam("userId") String userId) throws Exception
{
boolean response = new LampManager().turnOnLamp(userId);
return response;
}
Run Code Online (Sandbox Code Playgroud)
在我的客户端应用程序中,我这样做,它的工作原理:
String webPage = "http://localhost:8080/BliveServices/webresources/services.actuators/lampon";
URL urlToRequest = new URL(webPage);
//Authentication
urlConnection = (HttpURLConnection) urlToRequest.openConnection();
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(15000);
urlConnection.setRequestMethod("PUT");
urlConnection.setRequestProperty("Authorization", basicAuth);
urlConnection.setRequestProperty("Content-type", "multipart/form-data");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("userId", "2"));
(...)
Run Code Online (Sandbox Code Playgroud)
但是如何通过浏览器发送userId呢?
另一件事,我在构建项目时收到此消息:
SEVERE: Resource methods utilizing @FormParam and consuming "multipart/form-data" are no longer supported. See @FormDataParam.
Run Code Online (Sandbox Code Playgroud)
谢谢
如果要使用浏览器测试REST-Webservice,则必须安装插件.
如果您使用谷歌浏览器,您可以安装REST控制台我也使用这些插件来测试我的Web服务.
https://chrome.google.com/webstore/detail/rest-console/cokgbflfommojglbmbpenpphppikmonn
对于Firefox安装这些REST-Client
https://addons.mozilla.org/en-us/firefox/addon/restclient/
REST CLient也适用于Safari http://restclient.net/
对于Opera,您可以查看Simple REST-Client
https://addons.opera.com/en/extensions/details/simple-rest-client/
请尝试消费价值'application/x-www-form-urlencoded'
要从浏览器发出放置请求,您可以使用 jQuery 的jQuery.ajax(). (http://api.jquery.com/jQuery.ajax/)
例如:
$.ajax({
url: "test-url",
type: "PUT",
data: {userid: 1}
})
Run Code Online (Sandbox Code Playgroud)
将使用指定的数据向 test-url 发送 put-request。