N.M*_*M.N 10 java prototype jax-rs protocol-buffers mime-types
我需要从我创建的客户端将 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();
}
Run Code Online (Sandbox Code Playgroud)
我为此得到的输出是 Unsupported MediaType。
context=ClientResponse{method=PUT, uri=http://localhost:8080/ClientJAXRS/rest/Hello/proto, status=415, reason=Unsupported Media Type}}
Run Code Online (Sandbox Code Playgroud)
帮助我使用 proto 的 MIME TYPE 或发送的 proto 格式。
我在 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;
}
Run Code Online (Sandbox Code Playgroud)
使用 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);
}
}
Run Code Online (Sandbox Code Playgroud)
为 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();
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试从客户端使用这些端点
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);
Run Code Online (Sandbox Code Playgroud)
但回应是
ClientJAXRS/rest/Hello/proto-data, status=500, reason=Internal Server Error}}
text/html;charset=utf-
Run Code Online (Sandbox Code Playgroud)
你可以使用application/x-protobuf
OkHttp 请求体示例:
final RequestBody requestBody = RequestBody.create(MediaType.parse("application/x-protobuf"), builder.build().toByteArray());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1273 次 |
| 最近记录: |