REST/JSON : 如何生成样品请求?如何曝光API?

REST/JSON : How to generate sample requests ? How to expose API?

使用 java,当我用 Soap 公开一些 Web 服务时,我有一个描述所有 input/output 的 WSDL,如果我在我的 SoapUI 客户端中使用这个 WSDL,它会分析它并为我生成一些示例请求。

使用 Rest/Json 执行此操作的过程是什么。我知道 wadl,但 SoapUI 无法从中生成示例请求。我知道像 Swagger 套件这样的第三方工具,但这是唯一的方法吗?您是否必须使用一些外部文档工具来公开您的 API 并向用户展示一些示例请求?

 try {
           url="put your service url";
                HttpPost request = new HttpPost(url);
                request.setHeader("Accept", "application/json");
                request.setHeader("Content-type", "application/json");

                // Build JSON string
                JSONStringer item = new JSONStringer()
                        .object()
                        .key("password").value(pass)
                        .key("username").value(email)
                        .endObject();
                StringEntity entity = new StringEntity(item.toString());

                request.setEntity(entity);

                // Send request to WCF service
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpResponse response = httpClient.execute(request);
                HttpEntity entity1 = response.getEntity();
                InputStream stream = entity1.getContent();
                r = Splash.convertStreamToString(stream);
                    JSONObject jo = new JSONObject(r);
                s=  (Integer) jo.get("Flag");
                Log.d("json result is:", r);
            statusCode = response.getStatusLine().getStatusCode();

            } catch (Exception e) {
                e.printStackTrace();

            Log.d("error", "code"+0);
        }
        return s;
    }

还没有答案,所以这里是几年后的答案。

您需要使用 OpenAPI Specification(让我们称结果为 "Swagger contract"),它定义了一个标准的、与语言无关的 RESTful API 接口,而忘记 WADL。

这将是 SOAP WSDL 的等价物ui,但更易于阅读、更易于生成且约束更轻。

使用 Swagger,您可以在 "Contract first" 中工作(使用 https://editor.swagger.io/ to design the contract) or in "Code first", where you will use frameworks like Springfox 从代码 + 注释生成 swagger 合约。在我看来,最后一个要容易得多,它只是一种不同的方式"contract first",这不像你在设计合约之前就实现了整个应用程序。

在 URL 上获得 "swagger contract" 文档后,您可以部署一个 swagger-ui 网站以交互方式可视化它:它将生成一些示例请求并允许您在自定义后执行这些请求。