ast*_*eri 5 java rest web-services
免责声明:我确实搜索了这个问题的答案,是的,我确实找到了另一个问题:https://stackoverflow.com/questions/10315728/how-to-send-parameters-as-formparam-to-webservice.但首先,这个问题是询问Javascript,而我问的是Java,其次,它无论如何都没有答案.所以关于这个问题......
使用RESTful服务,将@QueryParams 传递到@GET服务中相当容易,因为您可以简单地将变量名称/值对附加到URL并使用它从程序中命中服务器.有没有办法用@FormParams 做这个?
例如,假设我有以下RESTful服务:
@POST
@Produces("application/xml")
@Path("/processInfo")
public String processInfo(@FormParam("userId") String userId,
@FormParam("deviceId") String deviceId,
@FormParam("comments") String comments) {
/*
* Process stuff and return
*/
}
Run Code Online (Sandbox Code Playgroud)
...让我说我的程序中的其他地方还有另一种方法,如下所示:
public void updateValues(String comments) {
String userId = getUserId();
String deviceId = getDeviceId();
/*
* Send the information to the /processInfo service
*/
}
Run Code Online (Sandbox Code Playgroud)
如何在第二种方法中执行注释掉的操作?
注意:假设这些方法不在同一个类或包中.还假设RESTful服务托管在与运行方法的计算机不同的服务器上.因此,您必须访问该方法并以RESTful方式传递值.
谢谢您的帮助!
使用@FormParam可以将表单参数绑定到变量.您可以在此处找到示例.
其次,为了在内部调用休息服务形成你的java方法代码,你必须使用jersey client.Example代码可以在这里找到.
您可以使用jersey客户端表单传递表单参数,如下所示.
创建表单
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(UriBuilder.fromUri("http://localhost:8080/api").build());
Form f = new Form();
f.add("userId", "foo");
f.add("deviceId", "bar");
f.add("comments", "Device");
Run Code Online (Sandbox Code Playgroud)
将它传递给Restful方法.
service.path("processInfo").accept(MediaType.APPLICATION_XML).post(String.class,f);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30137 次 |
| 最近记录: |