POST 复杂参数 REST 服务,能否使用模拟表单提交
POST complex parameters To REST service, Can I use a mock form submission
我最近问了这个问题:
我喜欢 restful 表示 link 顺便说一句的方式。问题本质上是如何为我的 REST 服务获取复杂参数?该代码的代码和参数是什么样的?好吧,我越想越让我想起一个简单的网络表单提交。请记住,此服务的客户端将是本机应用程序。为什么客户端应用程序 assemble 不能将问题中的变量放入 post 请求 Key-value object(包括一个字节 array-file),将其捆绑并发送到我的服务哪里会出现适当的 action/response?很确定 Java(RESTEasy 是我正在使用的框架)可以优雅地处理请求。我是疯了还是已经解决了?
举个例子,有没有人有一个示例 HTML 字符串来表示几个变量的简单 post,就像这样?
{
"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 headers 和所有???顺便说一句,我从这里得到了那个例子:
example JSON post
RestEasy
框架已经提供了 JAX-RS client
实现,除非您想使用 HttpURLConnection
甚至 Apache HttpComponents
中的 HttpClient
从头开始。
无论如何,只要问题与 RESTEasy 有关,我将提供后一个框架的示例。
如果 post 看起来像这样:
@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();
}
}
我最近问了这个问题:
我喜欢 restful 表示 link 顺便说一句的方式。问题本质上是如何为我的 REST 服务获取复杂参数?该代码的代码和参数是什么样的?好吧,我越想越让我想起一个简单的网络表单提交。请记住,此服务的客户端将是本机应用程序。为什么客户端应用程序 assemble 不能将问题中的变量放入 post 请求 Key-value object(包括一个字节 array-file),将其捆绑并发送到我的服务哪里会出现适当的 action/response?很确定 Java(RESTEasy 是我正在使用的框架)可以优雅地处理请求。我是疯了还是已经解决了?
举个例子,有没有人有一个示例 HTML 字符串来表示几个变量的简单 post,就像这样?
{
"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 headers 和所有???顺便说一句,我从这里得到了那个例子: example JSON post
RestEasy
框架已经提供了 JAX-RS client
实现,除非您想使用 HttpURLConnection
甚至 Apache HttpComponents
中的 HttpClient
从头开始。
无论如何,只要问题与 RESTEasy 有关,我将提供后一个框架的示例。
如果 post 看起来像这样:
@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();
}
}