【问题标题】:POST complex parameters To REST service, Can I use a mock form submissionPOST 复杂参数到 REST 服务,我可以使用模拟表单提交吗
【发布时间】:2016-07-17 09:38:37
【问题描述】:

我最近问了这个问题: Question I asked recently

顺便说一句,我喜欢链接表示的宁静方式。问题本质上是如何为我的 REST 服务获取复杂的参数?该代码的代码和参数是什么样的?好吧,我想得越多,它就越让我想起一个简单的网络表单提交。请记住,此服务的客户端将是本机应用程序。为什么客户端应用程序不能将问题中的变量组装到发布请求键值对象(包括字节数组文件)中,将其捆绑并将其发送到我的服务,在那里将发生适当的操作/响应?很确定 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 标头和所有的???顺便说一句,我从这里得到了那个例子: example JSON post

【问题讨论】:

    标签: json rest resteasy


    【解决方案1】:

    RestEasy 框架已经提供了一个JAX-RS client 实现,除非你想从头开始使用HttpURLConnection 甚至HttpClientApache HttpComponents 开始。 无论如何,只要问题与 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();
            }
            ...
    }
    

    basicRestEasy 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();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      • 2013-09-19
      • 2011-12-16
      相关资源
      最近更新 更多