Reputation: 2337
I've a protocol buffer service running on the remote machine say at https://website/protobufService
In the user guide I see only message structure. like
:
message Request {
required String name = 1;
required int id = 2,
........
}
message Response {
optional int result =1;
enum Code {
val1 = 1;
val2 = 2;
}
optional Code = 2;
}
I am not sure how to write the client code in Java to make use of this service ?
Is there any guidelines on writing the client code for Protobuf services ?
Thanks a ton in advance. - JE
Upvotes: 1
Views: 399
Reputation: 361
I ran into the same problem.I have written the below client for for my web service.Fortunately it does return an output.Unfortunately I think it is not in proto-buf format.I want to see it in protobuff format.I do not know how protobuff format looks like.You may wan to have a look at the below link:- https://spring.io/blog/2015/03/22/using-google-protocol-buffers-with-spring-mvc-based-rest-services
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpRestClient {
// http://localhost:8080/customers/
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:8080/customers/1");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/x-protobuf");
conn.setRequestProperty("Accept", "application/x-protobuf");
String contentType = conn.getContentType();
System.out.println(contentType);
System.out.println("---------------------------------------------------------------------");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " +
conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new
InputStreamReader((conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
Object response = output;
System.out.println(response);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
If anyone has better suggestions please lemme know.This is how my output looks like:-
Output from Server ....
Chris Richardson* [email protected]
As you could see it is not in protobuff format.
Upvotes: 1
Reputation: 45296
Protobufs only specify how to turn a message into bytes and back, not how to send it over a network. In particular, there is no standard way to send protobufs over HTTP. The documentation for the particular service you are trying to use needs to specify exactly how it expects the messages to be sent.
Typically, though, you would send an HTTP POST to the URL with an encoded Request
protobuf as its body, and it would respond with an encoded Response
protobuf. You should be able to use any old HTTP library to do this; just give in the encoded Request
as bytes, and decode the response bytes as a Response
.
Upvotes: 1