我需要从我创建的客户端向我的端点发送protobuf数据。下面是客户机和JAXRS端点的代码。
--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();
}我为此获得的输出是不支持的MediaType。
context=ClientResponse{method=PUT, uri=http://localhost:8080/ClientJAXRS/rest/Hello/proto, status=415, reason=Unsupported Media Type}}帮助我使用原语的MIME类型或发送的proto格式。
编辑2:
我在data.proto中为protobuf单独创建了一个文件
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;
}使用protoc -I代码在java中为它生成代码,并生成相册类。
为它实现了MessagebodyWriter和MessageBodyReader,如下所示
@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);
}
}为protobuf添加了2个端点如下所示
@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();
}我试图从客户端命中这些端点,使用
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);但他们的反应是
ClientJAXRS/rest/Hello/proto-data, status=500, reason=Internal Server Error}}
text/html;charset=utf-发布于 2021-02-01 19:44:32
您可以使用application/x-protobuf。
OkHttp请求体示例:
final RequestBody requestBody = RequestBody.create(MediaType.parse("application/x-protobuf"), builder.build().toByteArray());https://stackoverflow.com/questions/60757486
复制相似问题