I need to send protobuf data to my end point from a client I created. Below is the code for the Client and JAXRS endpoint.
--client
Client genClient = ClientBuilder.newClient();
WebTarget target2 = genClient.target("http://localhost:8080/ClientJAXRS/rest/Hello").path("/proto");
String inputproto = String.format("\n" + "syntax = \"proto3\";\n" +
"message Struct1 {\n" +
" string s1Att1 = 1;\n" +
" int32 s1Att2 = 2;\n" +
" int32 s1Att3 = 3;\n" +
" Struct2 s1Att4 = 4;\n" +
" message Struct2 {\n" +
" repeated string s2Att1 = 1;\n" +
" }\n" +
+ " ");
Response res = target2.request("application/x-protobuf").put(Entity.text(inputproto));
--endpoint
@PUT
@Path("/proto")
@Consumes("application/x-protobuf")
//@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getInfo(String text){
return Response.ok(text.getBytes(),MediaType.APPLICATION_OCTET_STREAM).status(200).build();
}
the output I get for this is Unsupported MediaType.
context=ClientResponse{method=PUT, uri=http://localhost:8080/ClientJAXRS/rest/Hello/proto, status=415, reason=Unsupported Media Type}}
Help me with the MIME TYPE for proto or the proto format that send.
I made a file separate for protobuf in data.proto
syntax = "proto3";
option java_outer_classname = "DataProtos";
option java_package = "com.client.JAXClient";
message Album {
optional string title = 1;
repeated string artist = 2;
repeated int32 release_year = 3;
required string song_title = 4;
}
Generated the code for it in java using protoc -I code and got Album class generated.
Implemented MessagebodyWriter and MessageBodyReader for it as given below
@Provider
@Produces("application/x-protobuf")
@Consumes("application/x-protobuf")
public class ProtoMessageBodyWriter implements MessageBodyWriter<Album>,MessageBodyReader<Album> {
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
// TODO Auto-generated method stub
return type == Album.class ;
}
@Override
public void writeTo(Album t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream out)
throws IOException, WebApplicationException {
Writer writer = new PrintWriter(out);
t.writeTo(out);
}
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
// TODO Auto-generated method stub
return type == Album.class ;
}
@Override
public Album readFrom(Class<Album> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream in)
throws IOException, WebApplicationException {
return Album.parseFrom(in);
}
}
Added 2 end points for protobuf as below
@GET
@Path("/proto-data")
@Consumes("application/x-protobuf")
public Response getInfo(Album inpAlbum){
StringBuilder sbuilder = new StringBuilder("Input album");
sbuilder.append("ID: ").append(inpAlbum.getReleaseYear()).append("\n");
sbuilder.append("Name: ").append(inpAlbum.getArtist()).append("\n");
return Response.created(null).entity(sbuilder.toString()).build();
}
I am trying to hit these endpoints from Client using
Response response =null;
Client client =null;
client = ClientBuilder.newClient();
WebTarget target2 = client.target("http://localhost:8081/ClientJAXRS/rest/Hello").path("/proto-data");
response= target2.request().get();
System.out.println(response);
But the response is
ClientJAXRS/rest/Hello/proto-data, status=500, reason=Internal Server Error}}
text/html;charset=utf-
you can use application/x-protobuf
OkHttp request body sample:
final RequestBody requestBody = RequestBody.create(MediaType.parse("application/x-protobuf"), builder.build().toByteArray());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With