首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将复杂参数发布到REST服务,我可以使用模拟表单提交吗?

将复杂参数发布到REST服务,我可以使用模拟表单提交吗?
EN

Stack Overflow用户
提问于 2016-07-17 01:00:25
回答 1查看 303关注 0票数 0

我最近问了这个问题:我最近问的问题

我喜欢restful的方式,该链接是代表BTW。问题本质上是如何为REST服务获取复杂的参数?该代码的代码和参数是什么样子的?嗯,我越想它,它就越让我想起一个简单的网页表单提交。请记住,此服务的客户端将是本地应用程序。为什么客户端应用程序不能将问题中的变量组装成post请求键值对象(包括字节数组文件),将其打包并发送到我的服务中,以便进行适当的操作/响应?非常肯定的是,Java (RESTEasy是我正在使用的框架)能够优雅地处理请求。我疯了还是这事已经解决了?

作为这种情况的一个例子,是否有人有一个示例HTML字符串来表示几个变量的简单帖子,如以下所示?

代码语言:javascript
复制
{
  "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帖子

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-17 01:38:37

RestEasy框架已经提供了一个JAX-RS client实现,除非您想从头开始使用HttpURLConnection,甚至使用Apache HttpComponentsHttpClient。无论如何,只要这个问题与RESTEasy有关,我就会提供一个关于后一个框架的例子。

如果帖子看起来是这样的话:

代码语言:javascript
复制
@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调用如下所示:

代码语言:javascript
复制
    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();
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38417001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档