繁体   English   中英

如何形成 http 请求主体的 protobuf 资源部分并通过 dhc 客户端或 postman 测试它以获得宁静的服务

[英]How to form protobuf resource part of http request body and test it through dhc client or postman for restful services

我已经创建了一个 .proto 消息,并且公开了一个 rest 服务,它看起来像这样

@Path("/test")
public interface test{

@POST
@Produces("application/x-protobuf")
@Consumes("application/x-protobuf")
public Response getProperties(TestRequest testrq);
}

现在 TestRequest 是 Java 生成的文件 of.protobuf 我如何在请求正文中传递它?

这将是 .proto 文件格式

message TestRequest
{
    string id = 1;
    string name = 2;
    enum TestType
    {
        Test=1
    }
   TestType testType = 3; 
}

如果您需要一个简单的 protobuf api 客户端,请尝试一下Protoman

免责声明:这是我的副业

您可以使用此代码片段来测试 protobuf,因为我找不到 postman 或 dhcclient 的任何解决方案

URL url = new URL("https://localhost:8080/test");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setDoInput(true);
urlc.setDoOutput(true);
urlc.setRequestMethod("POST");
urlc.setRequestProperty("Accept", "application/x-protobuf");
urlc.setRequestProperty("Content-Type","application/x-protobuf");
TestRequestPb.TestRequest.Builder testRequestBuilder = TestRequestPb.TestRequest.newBuilder();
TestRequest testRequest = testRequestBuilder.build();
testRequest.writeTo(urlc.getOutputStream());

testRequest = TestRequest.newBuilder().mergeFrom(urlc.getInputStream()).build();

您可以使用命令行工具protoCURL将 Protobuf 有效负载发送到您的 HTTP REST 端点并对其进行调试。 它的工作原理与 cURL 一样 - 除了您可以使用Protobuf 文本格式或 JSON 格式来描述您的请求,这将被自动编码和解码。 由于接受的答案实际上并未使用 postman 或 dhc 客户端,因此我认为 CLI 可能对您或其他读者来说是可以接受的。

在您的情况下,请求将如下所示:

protocurl -I path/to/proto -i ..TestRequest -o ..TestResponse \
  -u http://localhost:8080/test \
  -d "id: 'myid', name: 'myname', testType: Test"

假设 your.proto 文件位于文件夹path/to/proto中。

但是,因为您的 .proto 文件没有指定响应类型 TestResponse,所以无法显示它。 您需要将响应消息添加到您的 .proto 文件。 然而,即使没有其消息模式,也存在显示响应的未决问题

您可以在存储库中找到输出示例。

免责声明:我是这个的创造者并做到了,因为我遇到了同样的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM