我最近问了这个问题:我最近问的问题
我喜欢restful的方式,该链接是代表BTW。问题本质上是如何为REST服务获取复杂的参数?该代码的代码和参数是什么样子的?嗯,我越想它,它就越让我想起一个简单的网页表单提交。请记住,此服务的客户端将是本地应用程序。为什么客户端应用程序不能将问题中的变量组装成post请求键值对象(包括字节数组文件),将其打包并发送到我的服务中,以便进行适当的操作/响应?非常肯定的是,Java (RESTEasy是我正在使用的框架)能够优雅地处理请求。我疯了还是这事已经解决了?
作为这种情况的一个例子,是否有人有一个示例HTML字符串来表示几个变量的简单帖子,如以下所示?
{
"restriction-type": "boolean-search-restriction",
"boolean-logic": "and",
"restrictions": [
{
"restriction-type": "property-search-restriction",
"property": {
"name": "name",
"type": "STRING"
},
"match-mode": "EXACTLY_MATCHES",
"value": "admin"
},
{
"restriction-type": "property-search-restriction",
"property": {
"name": "email",
"type": "STRING"
},
"match-mode": "EXACTLY_MATCHES",
"value": "admin@example.com"
}
]
}但是使用html头和所有的??我从这里得到了这个例子,顺便说一句:示例JSON帖子
发布于 2016-07-17 01:38:37
RestEasy框架已经提供了一个JAX-RS client实现,除非您想从头开始使用HttpURLConnection,甚至使用Apache HttpComponents的HttpClient。无论如何,只要这个问题与RESTEasy有关,我就会提供一个关于后一个框架的例子。
如果帖子看起来是这样的话:
@Path("/client")
public class ClientResource {
@POST
@Consumes("application/json")
@Produces("application/json")
public Response addClient(Client aClient) {
String addMessage=clientService.save(aClient);
return Response.status(201).entity(addMessage).build();
}
...
}基本的RestEasy Client调用如下所示:
public void testClientPost() {
try {
ClientRequest request = new ClientRequest(
"http://localhost:8080/RestService/client");
request.accept("application/json");
Client client=new Client(5,"name","login","password");
//convert your object to json with Google gson
//https://github.com/google/gson
String input = gson.toJson(client);
request.body("application/json", input);
ClientResponse<String> response = request.post(String.class);
if (response.getStatus() != 201) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
//this is used to read the response.
BufferedReader br = new BufferedReader(new InputStreamReader(
new ByteArrayInputStream(response.getEntity().getBytes())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}https://stackoverflow.com/questions/38417001
复制相似问题