如何构建http请求体的protobuf资源部分并通过dhc客户端或邮递员进行测试以获得宁静的服务

时间:2016-09-04 08:30:00

标签: java rest postman protocol-buffers dhclient

我创建了一条.proto消息,并且我正在公开一个看起来像这样的休息服务

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

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

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

这将是.proto文件格式

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

2 个答案:

答案 0 :(得分:2)

您可以使用此代码段来测试protobuf,因为我找不到邮递员或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();

答案 1 :(得分:2)

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

免责声明:这是我的副项目